伟大的UIKit/Objective-C代码片段

Kei*_*ler 37 iphone objective-c uikit iphone-sdk-3.0

Objective-C iPhone/iPod touch/iPad开发的新手,但我开始在单行代码中发现很多功能,例如:

[UIApplication sharedApplication].applicationIconBadgeNumber = 10;
Run Code Online (Sandbox Code Playgroud)

这将在您的应用程序iphone上显示该特色红色通知徽章,编号为10.

请在这里为您的iPhone/iPod touch/iPad分享您最喜欢的Objective-C中的一个或两个内衬. 仅限公共API.

RRU*_*RUZ 39

在Safari中打开一个URL

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/"]];
Run Code Online (Sandbox Code Playgroud)

隐藏状态栏

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
Run Code Online (Sandbox Code Playgroud)

拨打电话号码(仅限iPhone)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://9662256888"]];
Run Code Online (Sandbox Code Playgroud)

启动Apple Mail

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://mymail@myserver.com"]];
Run Code Online (Sandbox Code Playgroud)

停止响应触摸事件

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
Run Code Online (Sandbox Code Playgroud)

积极的触摸事件

[[UIApplication sharedApplication] endIgnoringInteractionEvents];
Run Code Online (Sandbox Code Playgroud)

显示网络活动指标

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
Run Code Online (Sandbox Code Playgroud)

隐藏网络活动指示器

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
Run Code Online (Sandbox Code Playgroud)

防止iPhone进入睡眠模式

[UIApplication sharedApplication].idleTimerDisabled = YES;
Run Code Online (Sandbox Code Playgroud)


pro*_*rmr 14

  1. 显示警报窗口:

    UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"too many alerts" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show] 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 获取Documents文件夹的路径:

    NSArray*  paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDirectory = [paths objectAtIndex:0];
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将另一个视图控制器推入导航栏:

    [self.navigationController pushViewController:anotherVC animated:YES];
    
    Run Code Online (Sandbox Code Playgroud)
  4. 通过将alpha设置为0来淡化UIView:

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];  // fade away over 1 seconds
    [aView setAlpha:0]; 
    [UIView commitAnimations];                      
    
    Run Code Online (Sandbox Code Playgroud)
  5. 获取应用程序的名称

    self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
    
    Run Code Online (Sandbox Code Playgroud)
  6. 将状态栏更改为黑色

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
    
    Run Code Online (Sandbox Code Playgroud)
  7. 更改导航栏的样式(从视图控制器中):

    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    Run Code Online (Sandbox Code Playgroud)
  8. 将NSString保存到NSUserDefaults:

    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:loginName forKey:kUserLoginName];
    
    Run Code Online (Sandbox Code Playgroud)
  9. 从NSUserDefaults获取NSString:

    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    
    Run Code Online (Sandbox Code Playgroud)

    NSString*loginName = [defaults stringForKey:kUserLoginName];

  10. 在调用之前检查以确保对象支持方法:

    if ([item respondsToSelector:@selector(activateBOP:)]) {
        [item activateBOP:closeBOP];
    }
    
    Run Code Online (Sandbox Code Playgroud)
  11. 记录类和函数的名称:

    NSLog(@"%s", __PRETTY_FUNCTION__);
    
    Run Code Online (Sandbox Code Playgroud)
  12. 在任何UIView项目周围添加圆角和/或边框(个体经营)

    self.layer.borderColor  = [UIColor whiteColor].
    self.layer.cornerRadius = 8;     // rounded corners
    self.layer.masksToBounds = YES;  // prevent drawing outside border
    
    Run Code Online (Sandbox Code Playgroud)
  13. 打开Goog​​le地图应用,其中包含两个纬度/经度点之间的路线

    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]]; 
    
    Run Code Online (Sandbox Code Playgroud)


Gle*_*ith 10

将bool保存到用户默认值

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Yes Bool"];
Run Code Online (Sandbox Code Playgroud)

将文件从x复制到y

[[NSFileManager defaultManager] copyItemAtPath:x toPath:y error:nil];
Run Code Online (Sandbox Code Playgroud)

显示新视图

[self presentModalViewController:(UIViewController *) animated:YES];
Run Code Online (Sandbox Code Playgroud)

屏幕触摸方法

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}
Run Code Online (Sandbox Code Playgroud)

获取文档目录

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

加载网址

[MyWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://couleeapps.hostei.com"]]];  
Run Code Online (Sandbox Code Playgroud)

获取当前日期和时间:

NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
Run Code Online (Sandbox Code Playgroud)

自己的枚举类型:

typedef enum {
    a = 0, b = 1, c = 2
} enumName;
Run Code Online (Sandbox Code Playgroud)

石英画弧

CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGContextAddArc(ctxt, x, y, radius, startDeg, endDeg);
Run Code Online (Sandbox Code Playgroud)


Den*_*er9 8

使设备振动:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Run Code Online (Sandbox Code Playgroud)

使用特定电话号码打开消息应用:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:123456789"]];
Run Code Online (Sandbox Code Playgroud)

停止响应触摸事件:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
Run Code Online (Sandbox Code Playgroud)

再次开始回复:

[[UIApplication sharedApplication] endIgnoringInteractionEvents];
Run Code Online (Sandbox Code Playgroud)

最后,单行代码浏览器:

[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [urlText stringValue]]]];
Run Code Online (Sandbox Code Playgroud)

  • 这个页面上也有更多好的:http://getsetgames.com/tag/uiapplication (2认同)

Kev*_*vin 5

在UINavigationView上更改后退按钮上的标题.在推送视图之前,请在UINavigationController上使用此代码

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];


self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
Run Code Online (Sandbox Code Playgroud)