当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《伟大的UIKit/Objective-C代码片段》经验,为你挑选了5个好方法。

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

[UIApplication sharedApplication].applicationIconBadgeNumber = 10;

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

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



1> RRUZ..:

在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];

显示网络活动指标

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

隐藏网络活动指示器

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

防止iPhone进入睡眠模式

[UIApplication sharedApplication].idleTimerDisabled = YES;



2> progrmr..:

    显示警报窗口:

    UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"too many alerts" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show] 
    

    获取Documents文件夹的路径:

    NSArray*  paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDirectory = [paths objectAtIndex:0];
    

    将另一个视图控制器推入导航栏:

    [self.navigationController pushViewController:anotherVC animated:YES];
    

    通过将alpha设置为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];
    }
    

    记录类和函数的名称:

    NSLog(@"%s", __PRETTY_FUNCTION__);
    

    在任何UIView项目周围添加圆角和/或边框(个体经营)

    self.layer.borderColor  = [UIColor whiteColor].
    self.layer.cornerRadius = 8;     // rounded corners
    self.layer.masksToBounds = YES;  // prevent drawing outside border
    

    打开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]]; 
    



3> Glenn Smith..:

将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];

加载网址

[MyWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://couleeapps.hostei.com"]]];  

获取当前日期和时间:

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

自己的枚举类型:

typedef enum {
    a = 0, b = 1, c = 2
} enumName;

石英画弧

CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGContextAddArc(ctxt, x, y, radius, startDeg, endDeg);



4> DenverCoder9..:

使设备振动:

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]]]];


这个页面上也有更多好的:http://getsetgames.com/tag/uiapplication

5> Kevin..:

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

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


self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];

推荐阅读
跟我搞对象吧
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有