很棒的UIKit / Objective-C代码片段
Objective-C iPhone / iPod touch / iPad开发新手,但是我开始发现一些代码如下:
[UIApplication sharedApplication].applicationIconBadgeNumber = 10; 这将显示在您的应用程序iPhone 10号的独特的红色通知徽章。
请在这里与iPhone / iPod touch / iPad分享您在Objective-C中最喜欢的一两行。 只有PUBLIC API 。
在Safari中打开一个URL
 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/"]]; 
隐藏状态栏
 [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO]; 
拨打电话号码(仅限iPhone)
 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://9662256888"]]; 
启动Apple Mail
 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://mymail@myserver.com"]]; 
停止响应触摸事件
 [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 
激活触摸事件
 [[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
显示networking活动指示器
 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
隐藏networking活动指示器
 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
防止iPhone进入睡眠模式
 [UIApplication sharedApplication].idleTimerDisabled = YES; 
- 
显示一个警报窗口: UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"too many alerts" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [alert show]
- 
获取“文档”文件夹的path: NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0];
- 
将另一个视图控制器推到导航栏上: [self.navigationController pushViewController:anotherVC animated:YES];
- 
通过将alphaanimation设置为0来淡出UIView: [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1]; // fade away over 1 seconds [aView setAlpha:0]; [UIView commitAnimations];
- 
获取应用程序的名称 self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
- 
将状态栏更改为黑色 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
- 
更改导航栏的风格(从视图控制器中): self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
- 
将一个NSString保存到NSUserDefaults中: NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:loginName forKey:kUserLoginName];
- 
从NSUserDefaults获取一个NSString: NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];NSString * loginName = [defaults stringForKey:kUserLoginName]; 
- 
在调用它之前检查以确保对象支持一种方法: if ([item respondsToSelector:@selector(activateBOP:)]) { [item activateBOP:closeBOP]; }
- 
logging类和函数的名称: NSLog(@"%s", __PRETTY_FUNCTION__);
- 
在任何UIView项目(自我)周围添加圆angular和/或边框 self.layer.borderColor = [UIColor whiteColor]. self.layer.cornerRadius = 8; // rounded corners self.layer.masksToBounds = YES; // prevent drawing outside border
- 
打开两个纬度/长点之间的方向的Google地图应用程序 NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirflg=d", start.latitude, start.longitude, finish.latitude, finish.longitude]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
将bool保存到用户默认值
 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Yes Bool"]; 
将文件从x复制到y
 [[NSFileManager defaultManager] copyItemAtPath:x toPath:y error:nil]; 
显示一个新的视图
 [self presentModalViewController:(UIViewController *) animated:YES]; 
屏幕触摸方法
 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {} 
获取文档目录
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; 
加载URL
 [MyWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://couleeapps.hostei.com"]]]; 
获取当前date和时间:
 NSCalendar *gregorian = [NSCalendar currentCalendar]; NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]]; 
自己的枚举types:
 typedef enum { a = 0, b = 1, c = 2 } enumName; 
石英画弧
 CGContextRef ctxt = UIGraphicsGetCurrentContext(); CGContextAddArc(ctxt, x, y, radius, startDeg, endDeg); 
使设备振动:
 AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 
使用特定的电话号码打开留言应用程序:
 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:123456789"]]; 
停止响应触摸事件:
 [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 
再次开始回应:
 [[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
最后, 单行代码浏览器 :
 [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [urlText stringValue]]]]; 
更改UINavigationView上后退button上的标题。 在推送视图之前,在UINavigationController上使用此代码
 UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil]; self.navigationItem.backBarButtonItem = backBarButtonItem; [backBarButtonItem release];