在 iOS 開發(fā)中我們經(jīng)常會用到但是容易遺忘的代碼持寄,下面做一個總結(jié),以備忘記的時候回來翻看:
一娱俺、在 APPDelegate 中稍味,需要手寫代碼對首頁的控制器進行導(dǎo)航欄推出的時候的代碼片段:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 更改window的顏色為白色
self.window.backgroundColor = [UIColor whiteColor];
/*
// 需要跳轉(zhuǎn)控制器的對象
MainViewController *mainVC = [[MainViewController alloc] init];
// 讓MainViewController成為導(dǎo)航控制器的根視圖
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mainVC];
*/
// 以上兩句可以合成下面一句完成
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] init]];
// 讓導(dǎo)航控制器成為window的根視圖
self.window.rootViewController = nav;
// 讓window成為主窗口并可見
[self.window makeKeyAndVisible];
return YES;
}
二、日期和字符串之間的轉(zhuǎn)換
// 從服務(wù)器段獲取到的字符串轉(zhuǎn)化為時間如:轉(zhuǎn)化 1416882712000
NSString *dateString = @"1416882712000";
// 先把字符串轉(zhuǎn)換為int形式,如果是毫秒需要把轉(zhuǎn)化后的int值除以1000,是秒的話則不需要除
NSInteger timeInterval = [dateString integerValue] / 1000;
// 進行日期的計算荠卷,以1970年為基準(zhǔn)
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSLog(@"%@",date);
// 以上輸出為: 2014-11-25 02:31:52 +0000
// 進行日期的格式化輸出
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// 需要輸出的日期格式:如:yyyy-MM-dd HH:mm:ss 或 yyyy年MM月dd日 HH時mm分ss秒
// dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// NSString *dateStr = [dateFormatter stringFromDate:date];
// NSLog(@"%@",dateStr);
// 以上輸出為: 2014-11-25 02:31:52
// 更換日期格式則輸出:2014年11月25日 10時31分52秒
dateFormatter.dateFormat = @"yyyy年MM月dd日 HH時mm分ss秒";
NSString *dateStr = [dateFormatter stringFromDate:date];
NSLog(@"%@",dateStr);
三模庐、日期字符串轉(zhuǎn)換為日期
// 將一個字符串如“20110826134106”轉(zhuǎn)化為任意的日期時間格式
NSString *string = @"20110826134106";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[formatter setDateFormat:@"yyyyMMddHHmmss"];
NSDate *date = [formatter dateFromString:string];// 日期類型
NSLog(@"date = %@",date);
// 輸出結(jié)果:date = 2011-08-26 05:41:06 +0000
// 以上輸出的會比需要輸出的少8小時
// NSInteger timeInterval = 8 * 60 * 60;
// NSDate *date1 = [NSDate dateWithTimeInterval:timeInterval sinceDate:date];// 日期類型
// NSLog(@"date1 = %@",date1);
// 輸出結(jié)果: date1 = 2011-08-26 13:41:06 +0000
// 以上輸出的就為我們需要的日期時間
// 直接轉(zhuǎn)換字符串就不需要加8小時的時間間隔
// formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// NSString *targetDateString = [formatter stringFromDate:date];// 字符串類型
// NSLog(@"targetDateString = %@",targetDateString);
// 輸出結(jié)果:targetDateString = 2011-08-26 13:41:06
NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[NSLocale currentLocale]];
[outputFormatter setDateFormat:@"yyyy年MM月dd日 HH時mm分ss秒"];
NSString *str= [outputFormatter stringFromDate:date];
NSLog(@"testDate:%@",str);
日期格式引用
iOS-NSDateFormatter 格式說明(轉(zhuǎn)載)
格式化參數(shù)如下:
G: 公元時代,例如AD公元
yy: 年的后2位
yyyy: 完整年
MM: 月油宜,顯示為1-12
MMM: 月掂碱,顯示為英文月份簡寫,如 Jan
MMMM: 月怜姿,顯示為英文月份全稱,如 Janualy
dd: 日疼燥,2位數(shù)表示沧卢,如02
d: 日,1-2位顯示醉者,如 2
EEE: 簡寫星期幾但狭,如Sun
EEEE: 全寫星期幾,如Sunday
aa: 上下午撬即,AM/PM
H: 時立磁,24小時制,0-23
K:時剥槐,12小時制唱歧,0-11
m: 分,1-2位
mm: 分粒竖,2位
s: 秒颅崩,1-2位
ss: 秒,2位
S: 毫秒
常用日期結(jié)構(gòu):
yyyy-MM-dd HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyyy
四温圆、設(shè)置圓形按鈕
代碼設(shè)置:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(CGRectGetMidX(self.view.frame) - 100, CGRectGetMidY(self.view.frame) - 100, 200, 200);
btn.backgroundColor = [UIColor magentaColor];
[btn setTitle:@"click me" forState:UIControlStateNormal];
btn.layer.masksToBounds = YES;
btn.layer.cornerRadius = CGRectGetHeight(btn.frame) / 2.0f;
[self.view addSubview:btn];
運行截圖: