目錄:web
回到頂部url
1. UIDatePickerspa
1.1 獲取用戶選擇的日期:代理
NSDate *date = [UIDatePicker date];code
1.2 須要設置的屬性:
Mode: 時間, 日期, 日期和時間…
Locale: 設置地區,好比中文等
Date: 初始日期,通常爲當前日期
1 NSDate *selectedDate = [self.datePicke date]; 2 3 //將世界標準時間 轉換爲 本地時間 4 5 //建立時間格式對象 6 7 NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 8 9 //設置時間格式 10 11 dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; 12 13 //調用stringFromDate方法將當前時間(或用戶選擇的時間)轉換爲本地時間 14 15 NSString* str = [dateFormatter stringFromDate:selectedDate]; 16 17 self.dateLabel.text = [NSString stringWithFormat:@"%@",str];
2. UIPickerView
2.1 功能:
讓用戶在列表中選擇一條數據
2.2 數據來源:
UIPickerViewDatasource, UIPickerViewDelegate
須要設置數據源和委託,回答問題
多少列(Component), 每列多少行(Row)
每行的內容:
能夠是NSString字符串
也能夠是NSAttributeString屬性字符串
更能夠是UIView的視圖對象
2.3 選擇後的響應方法
1 // 有多少列 2 3 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 4 5 return 1; 6 7 } 8 9 // 每一列有多少行 10 11 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 12 13 return self.areas.count; 14 15 } 16 17 // 每一行的內容,這個方法是在代理中 18 19 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 20 21 return self.areas[row]; 22 23 } 24 25 // 獲取選擇了哪個 26 27 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 28 29 self.label.text = self.areas[row]; 30 31 }
3. UIPickerView多列關聯
3.1 關聯的關鍵是將數據模型關聯起來
areas: NSDictionary: (key :NSString, value:NSArray)
cites: NSArray (keys)
regions : NSArray (valueForKey)
3.2 當選擇城市時,調用方法,方法中將regions中的數據換掉,而後刷新界面:
pickerView reloadComponent:
3.2 代碼方式選擇某一行:
pickerView selectRow:inComponent:animated:
1 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 2 3 if (component == 0) { 4 5 self.regions = [self.areas valueForKey:self.cities[row]]; 6 7 [pickerView reloadComponent:1]; 8 9 [pickerView selectRow:0 inComponent:1 animated:YES]; 10 11 } 12 13 }
4. UIWebView
4.1 屬性:
Scaling Page to Fit 選中
4.2 加載網頁:
NSURL : http://www.apple.com.cn
NSURLRequest
webView loadRequest:
1 - (IBAction)goWeb:(UITextField *)sender { 2 3 NSURL *url = [NSURL URLWithString:sender.text]; 4 5 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 6 7 [self.webView loadRequest:request]; 8 9 }
5. Storyboard(故事板)
5.1 是什麼
是一堆nib文件,放到一個文件中
5.2 幹什麼
管理全部的界面相關的信息以及界面與界面之間的跳轉
5.3 基本概念
自iOS5開始, Xcode5中建立項目默認使用Storyboard
替代nib文件(95%)
Storyboard xib
Scene(場景) 一個xib文件
initWithCoder: initWithNibName:bundle:
5.4 好處
減小代碼量
直觀,清晰
全部的View集中到一塊兒
不須要代碼指定rootViewController, 由Storyboard自動完成
5.5 VC-Modal跳轉
1)拖拽一個VC(黃球)
2)在舊場景中的按鈕上拉線到新場景,建立Segua,選擇Modal模式
能夠選擇建好的Segua, 設置其屬性:
Identifier : 標識符,用於區別多個Segua
Style : 可選擇跳轉的模式(Modal或push)
Transition: 選擇跳轉的動畫
3)新界面-VC
若是新界面不須要寫代碼, 那也就不須要建立新的類與新場景綁定
若是新界面中須要寫代碼控制,那就須要建立新類:
1)建立一個繼承自UIViewController的類
注意不要選擇with xib for user interface
2)點擊新場景,設置類綁定
3) 其餘和之前同樣,如設置返回按鈕:
4) 界面返回:
千萬不要用連線的方式返回
鏈接一個方法,dismiss…
5.6 VC-push
1) 須要Navigation包含:
選擇須要包含的場景
Editor-->Embed In --> Navigation Controller
2) 從舊場景中的按鈕上拉線到新場景,建立segua, 選擇push模式
至關於從NavigationController中push了一個新場景
5.7 Navigation和Modal一塊兒使用
從一個Navigation跳轉到另外一個Navigation,應該使用Modal模式
5.8 正向傳值
1) 作爲跳轉連線(建立Segue)
2) 接收參數的VC(Segue的目的地):
1> 拖拽顯示控件到界面,增長屬性
2> 重寫viewWillAppear,根據數據刷新顯示
3> 定義公開屬性用來接收傳入的參數
3) 設置Segue 的標準符:
在Storyboard中找到Segue, 打開檢查器4, 設置Identifier屬性,起個名字
4)重寫 prepareForSegue 方法,進行傳值
1>打開源VC代碼
2>重寫prepareForSegue
3>使用segue.identifier屬性區分哪一個Segua
4> 使用segue.destinationViewController屬性獲取目標VC
5> 給目標VC中的屬性賦值,達到傳參的目的。
5) 若是目標VC被包含在一個Navigation中,則拿到的目標VC是navi,此時須要以下操做纔可獲得真正的目標VC:
1 // 在segue跳轉前自動調用此方法,只要segue跳轉都會調用此方法,正向傳值 2 3 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 4 5 if ([segue.identifier isEqualToString:@"GoCViewController"]) { 6 7 NSString *parameter = @"我是參數"; 8 9 // 目的地VC 10 11 MXCViewController *destination = segue.destinationViewController; 12 13 destination.data = parameter;//傳參 14 15 } 16 17 if ([segue.identifier isEqualToString:@"GoNaviViewController"]) { 18 19 NSString *parameter = @"000"; 20 21 UINavigationController *navi = segue.destinationViewController; 22 23 MXBViewController *destination = (MXBViewController *)navi.topViewController; 24 25 destination.data = parameter; 26 27 } 28 29 }
5.9 Navigation配置
1) title
直接點擊場景上的NavigationBar, 編輯
2) navigation item (左右按鈕)
一個按鈕直接拖拽
多個按鈕:
只能用代碼,跟之前同樣
3)toolbar
1>設置顯示
選擇NavigationController
檢查器4 選中 Show ToolBar
2>給被包含的VC加按鈕
拖拽
3>不一樣的VC能夠配置不一樣的Toolbar
若是沒有配置VC的toolbar時,不但願push時顯示出來:
選中VC,檢查器4: Hide Bottom Bar On Push
4)自定義頭視圖
直接拖拽一些控件到NavigationBar上使用
或者拖拽一個UIView作爲頭視圖,UIView中能夠隨便佈局子視圖
之前的xib方式下,用代碼寫:
self.navigationItem.titleView = …..;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:UIView];
5.10 TabBarController
1)第一方式
拖拽一個Tabbar黃球
刪除不須要的子場景
接線從TabBarController到你要的場景中,注意要選擇view controllers
2) 第二方式
選擇須要包含的VC
Editor-->Embed In --> Tab Bar Controller
3) 配置
title, image
點擊具體的VC下的TabBar, 修改Title, 檢查器4中選擇image
5.11 和舊的xib代碼兼容
在Storyboard中跳轉到xib文件:
連線IBAction, 調用方法,方法中和之前同樣:
建立VC對象(initWithNibName:bundle:), 而後present或push
在Storyboard中不使用Segue來進行代碼跳轉到其餘場景:
在要跳轉的那個VC中設置一個屬性storyboard ID(第三個檢查器),而後使用present進行跳轉
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyScene"];
[self presentViewController:viewController animated:YES completion:nil];
補充:
arc4random() % 5 隨機產生0~4其中一個