iOS -- 开发技巧(二)

本文将全面介绍iOS开发的基本概念、关键技术、实战案例和最佳实践,旨在帮助开发者快速掌握iOS应用开发技能,从零开始打造高质量的iOS应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、获取全局的Delegate对象,这样我们可以调用这个对象里的方法和变量:

[(MyAppDelegate*)[[UIApplication sharedApplication] delegate] MyMethodOrMyVariable];

2、获得程序的主Bundle:

NSBundle *bundle = [NSBundle mainBundle];

Bundle可以理解成一种文件夹,其内容遵循特定的框架。

Main Bundle一种主要用途是使用程序中的资源文件,如图片、声音、plst文件等。

NSURL *plistURL = [bundle URLForResource:@"plistFile" withExtension:@"plist"];

上面的代码获得plistFile.plist文件的路径。

3、在程序中播放声音:

首先在程序添加AudioToolbox:

其次,在有播放声音方法的.m方法添加#import:

#import<AudioToolbox/AudioToolbox.h>

接下来,播放声音的代码如下:

NSString *path = [[NSBundle mainBundle] pathForResource:@"soundFileName" ofType:@"wav"]; 

SystemSoundID soundID; 

AudioServicesCreateSystemSoundID ((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID); 

AudioServicesPlaySystemSound (soundID); 

4、设置和获取类中属性值:

[self setValue: 变量值 forKey: 变量名];

[self valueForKey: 变量名];

5、让某一方法在未来某段时间之后执行:

[self performSelector:@selector(方法名) withObject:nil afterDelay:延迟时间(s)]; 

6、获得设备版本号:

float version = [[[UIDevice currentDevice] systemVersion] floatValue];

7、捕捉程序关闭或者进入后台事件:

UIApplication *app = [UIApplication sharedApplication];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];

applicationWillResignActive:这个方法中添加想要的操作

8、查看设备支持的字体:

for (NSString *family in [UIFont familyNames]) {

    NSLog(@"%@", family);

    for (NSString *font in [UIFont fontNamesForFamilyName:family]) {

        NSLog(@"\t%@", font);

    }

}

9、为UIImageView添加单击事件:

imageView.userInteractionEnabled = YES;

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];

[imageView addGestureRecognizer:singleTap];

10、添加多语言支持: 比如Image Picker这样的组件,它上面的按钮的文字是随着设备语言环境的改变而改变的,但是要先在工程添加语言:

11、使程序支持iTunes这样的设备,比如可以使用PC端的工具往程序的Documents中拖放文件。

12、页面切换效果设置:

controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentModalViewController:controller animated:YES];

可供使用的效果:

UIModalTransitionStyleCoverVertical

UIModalTransitionStyleFlipHorizontal

UIModalTransitionStyleCrossDissolve

UIModalTransitionStylePartialCurl

恢复之前的页面:

[self dismissModalViewControllerAnimated:YES];


(累计添加)附:

1.判断当前设备是iPad还是iPhone\iPod

     
  1. if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
  2. NSLog(@"The Device is a iPad!");
  3. }else{
  4. NSLog(@"Not a iPad the device!");
  5. }
  6. 2.导入旧工程,解决Xcode4.5以后模拟器屏幕不旋转问题

    if ([self.window respondsToSelector:@selector(setRootViewController:)]) {

            self.window.rootViewController = navigation;

        } else {

            [self.window addSubview:navigation.view];

        }

    或者

     
  1. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0){
  2. self.window.rootViewController = navigationCtrl;
  3. }else{
  4. [self.window addSubview:navigationCtrl.view];
  5. }

3.支持iPhone5:添加Retina4 launch image"Default-568h@2x.png"

图片尺寸:

     
  1. Default.png 320x480
  2. Default@2x.png 640x960
  3. Default-568h@2x.png 640x1136

4.根据iPhone5的宽和高,判断当前设备是不是iPhone5

     
  1. CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
  2. CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
  3. if ((screenWidth==568)||(screenHeight==568)) {
  4. isiPhone5 = YES;
  5. }

注意:iPhone5的高是568,取得屏幕大小用

     
  1. [UIScreen mainScreen].bounds

  • 5.退出程序的代码
    1. - (void)exitApplication {  //退出方法,为推出加动画效果  
    2.     [UIView beginAnimations:@"exitApplication" context:nil];    
    3.     [UIView setAnimationDuration:0.5];    
    4.     [UIView setAnimationDelegate:self];    
    5.     [UIView setAnimationTransition:UIViewAnimationCurveEaseOut forView:self.window cache:NO];    
    6.     [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];    
    7.     self.window.bounds = CGRectMake(0, 0, 0, 0);    
    8.     [UIView commitAnimations];    
    9. }  
    10. - (void)animationFinished:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {    
    11.     if ([animationID isEqualToString:@"exitApplication"]) {    
    12.         exit(0);  //这句是退出的执行代码  
    13.     }  
    14. }  

    6.判断程序是否是第一次运行

    appdelegate.m中找到 “application:didFinishLaunchingWithOptions:”方法添加以下代码:

    [html]  view plain copy
    1. if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {  
    2.     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];  
    3.     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];  
    4. }  
    5. else{  
    6.     [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];  
    7. }  

    (这里必须用两个userdefault如果用一个只能达到开关效果)

    总的解决办法是 2 key: @”everLaunched”判断用户以前是否登录

    @”firstLaunch” 用来开发者在程序的其他部分判断

    在第一次启动的时候 key @”everLaunched” 不会被赋址的并且设置为YES. @”firstLaunch”被设置为 YES.

    在程序的其他部分用以下代码判断:

    [html]  view plain copy
    1. if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {  
    2.     // 这里判断是否第一次  
    3.     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"第一次"   
    4.                                                   message:@"进入App"  
    5.                                                  delegate:self   
    6.                                         cancelButtonTitle:@"我知道了"   
    7.                                         otherButtonTitles:nil];  
    8.     [alert show];  
    9.     [alert release];  
    10.       
    11. }  

    第一段代码运行key @”firstLaunch” 将被设置为 NO。 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值