IOS Cheat List

1.页面切换

//页面切换
MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentModalViewController:controller animated:YES];
//页面返回
[self dismissModalViewControllerAnimated:YES];

2.获取AppDelegate

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

3.获取配置信息

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *MyTextValue = [defaults objectForKey:TEXT_VALUE_KEY];

BOOL MyBoolValue = [defaults boolForKey:BOOL_VALUE_KEY];

4.读取plist

//本地
NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
//远程
NSStrng * strURL = [[NSString alloc] initWithFormat:@"FORMAT",.....];
NSLog(@"%@",strURL);
NSURL *plistURL = [NSURL URLWithString:strURL];
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfURL:plistURL];

5.关闭键盘

[myControl resignFirstResponder];

6.内置功能调用

//网站
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://网址"]];
//打电话
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://电话号码"]];
//发送邮件
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://邮箱地址"]];
//发送短信
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://电话号码"]];

7.导航

//左按钮
UIBarButtonItem *lBtn = [[UIBarButtonItem alloc] initWithTitle:@"名称" style:UIBarButtonItemStylePlain target:self action:@selector(lBtnPressed:)];
self.navigationItem.leftBarButtonItem = lBtn;
 [lBtn release];

//入栈
MyViewController *nextController = [[MyViewController alloc] init];
nextController.title = @"MyViewControllerName";
[self.navigationController pushViewController:nextController animated:YES];

//出栈
[self.navigationController popViewControllerAnimated:YES];

8.返回一个TableViewCell

static NSString *tableViewCellIdentifier = @"MyCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
if(cell==nil)
{
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewCellIdentifier] autorelease];
}
cell.textLabel.text = @"TEXT"
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;

9.UIAlertView异步的哟

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"HI" message:@"Hello" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];

10.注册推送通知

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeSound];

11.注册网络状态

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: KEY_ATS_NETWORK_CHANGE_NOTIFICATION object: nil];

NSString* token = [deviceToken description];
deviceTokenId = [[[token stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" "withString:@""] retain];

PushedMsg *newMsg = [[PushedMsg alloc] init];
newMsg.msgContent = [[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] copy];

12.本地通知

//增加
UILocalNotification *notification=[[UILocalNotification alloc] init];
NSDate *now1=[NSDate date];
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.repeatInterval=NSDayCalendarUnit;
notification.applicationIconBadgeNumber = 1;
notification.alertAction = NSLocalizedString(@"显示", nil);

notification.fireDate=[now1 dateByAddingTimeInterval:10];
notification.alertBody=@"通知";

[notification setSoundName:UILocalNotificationDefaultSoundName];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%d",myswitch.tag],KEY_ATS_LOCAL_NOTIFICATION, nil];
[notification setUserInfo:dict];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//取消
UILocalNotification *myUILocalNotification=[myArray objectAtIndex:i];
if ([[[myUILocalNotification userInfo] objectForKey:KEY_ATS_LOCAL_NOTIFICATION] intValue]==myswitch.tag)
{
[[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];
}

13.计算MD5

#import <CommonCrypto/CommonDigest.h>

//生成指定字符串的MD5
+ (NSString*)CalcMD5:(NSString *)InString
{
    //生成MD5
    const char *ptr = [InString UTF8String];
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(ptr, strlen(ptr), md5Buffer);
   
    //转为NSString
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    {
        &#91;output appendFormat:@"%02x",md5Buffer&#91;i&#93;&#93;;
    }
    return output;
}
&#91;/code&#93;

<strong>14.获取位置</strong>
[code lang="objc"]
CLLocationManager* locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

15.本地化

NSLocalizedString(@"KEY",@"DEFAULT");

16.获取图片

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];

17.加速器

UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate =self;
accel.updateInterval = UPDATE_INTERVAL;

18.声音

NSString *path=[[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);

AudioServicesPlaySystemSound(soundID);

Leave a Reply

Your email address will not be published. Required fields are marked *

*