標籤:
一、知識點
1、介紹資料選擇控制項UIPickerView和日期選擇控制項UIDatePicker控制項
* UIPickerView的案例
* 點餐系統
* 城市選擇
* 國旗選擇
* UIDatePicker的案例
* UIToolbar的介紹和基本使用
* UIBarButtonItem的介紹和示範
* 將日期或時間資料設定到文字框上
2、項目設定檔介紹
* 應用頭像
* 應用載入圖片的設定
* info.plist檔案介紹:
* 常用的key
* Xcode 6以前都是以“項目名稱-info.plist”命名,Xcode 6以後就只有info.plist
// 現在曆史版本Xcode的網址
// https://developer.apple.com/downloads/
* 通過open as source code查看info.plist中完整的key
* 通過代碼去擷取info.plist中的資訊
* pch檔案介紹
* 建立pch檔案
* 配置基本資料
* 列印宏的示範
二、UIPickerView的使用
- 作用:
- 從指定的“資料來源”中選擇資料
- 通常以UITextField的“inputView”出現,當選中的某個文字框時,彈出的鍵盤中顯示該控制項。
- 注意:
- 使用PickerView之前需要指定資料來源對象和代理對象
- 需要使用到的兩個協議
- UIPickerViewDataSource 資料來源協議
- UIPickerViewDelegate 代理協議
1、資料來源方法
設定列:
numberOfComponentsInPickerView:(UIPickerView *)pickerView
設定行:
pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
2、delegate代理方法
設定顯示內容(titleForRow)
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
也可以設定顯示視圖(viewForRow)
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
擷取pickerview選擇的資料方法:(didSelectRow)
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
三、隨機數產生(arc4random_uniform)
arc4random():c語言函數,隨機產生一個不帶正負號的整數(0-2的32次)
arc4random()%(n+1) 隨機產生一個0-n之間的數字
arc4random_uniform(256) 蘋果推薦,隨機產生一個0-n之間的數字
(隨機產生顏色:
[UIColor colorWithRed :arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue: arc4random_uniform(256)/255.0 alpha:1.0];
有個問題:隨機產生的數可能和當前選擇的數一致,導致pickerview沒有發生改變:
解決:
1、擷取當前選擇項:
//擷取當前選中的行號,如果和隨機數相同,則重建隨機數
NSInteger selRow = [self.pickerview selectedRowInComponent:i];
2、和隨機數進行比較,如果一致,則重建隨機數
//如果不一樣則重建隨機數
randomNum =arc4random_uniform((int)count);
3、如果不一致,則之間顯示隨機數
[_pickerview selectRow:randomNum inComponent:i animated:true];
四、城市選擇(第0列的選擇會影響第1列的資料顯示)
1、重點:怎麼顯示行:
判斷,如果是第0列的話:每行顯示的就是省名稱:
if(component= =0)
{
province =self.dataArray[row];
return province.name;
}
判斷如果不是第0列的話:
a、擷取選定的省的數組下標(在第0列的下標)
NSInteger selProIdx =[pickerView selectedRowInComponent:0];
b、擷取選定省
privince =self.dataArray[selProIdx]
c、擷取選定省所包含的城市數組
NSArray *citiesAttay =province.cities;
d、載入城市名稱並顯示
return citiesAttay[row];
2、重點:發現一個問題:就是城市這一列的資料,在沒滾動前,不會自動更新,需要手動重新整理
在didSelectRow中進行資料更新和顯示 並使用reloadComponent選擇要重新整理的列
/如果滾動的是第0列,要重新整理第一列城市資料
if(component==0)
{
[pickerView reloadComponent:1];
//選中第一列第0行
[pickerView selectRow:0 inComponent:1 animated:YES];
}
3、UIPickerView 的重要Bug:數組越界導致程式崩潰
/**
崩潰原因:
第1次選中的省,顯示的城市資料固定
再次滾動的時候,如果沒有停止,城市的資料還是原來的
但是,停止以後就要重新整理城市的資料,這個時候,原來的城市數組跟當前要顯示的城市資料不一致,導致數組越界崩潰
解決:
在計算顯示有多少行的時候,將省模型儲存起來,以後都通過儲存的省來顯示,擷取資料顯示
*/
在
numberOfRowsInComponent:解決:
//2、根據下標找到省模型
CZProvince *province =self.dataArray[selProIdx];
//儲存上一次顯示的行數
self.selPro =province;
return self.selPro.cities.count;
在此後對cities的調用選取中,都直接通過:
return self.selPro.cities[row];
的方法直接顯示擷取資料
五、國家選擇(UIPickerView中顯示的是自訂view的情況)
1、通過xib建立視圖
2、通過flagview方法返回視圖/行高
+(instancetype)flagView
{
return [[[NSBundle mainBundle] loadNibNamed:@"CZFlagView" owner:nil options:nil] lastObject];
}
//行高由誰決定就由誰去管理
+(CGFloat) rowHeight
{
return 80;
}
3、擷取視圖或行高(在-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 方法中:以及rowHeightForComponent方法中
CZFlagView *flagview =[CZFlagView flagView];
#pragma mark -怎麼可變的改變行高 根據xib 在view裡面再定義一個方法,直接告訴行高,並在內部實現
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return [CZFlagView rowHeight];
}
六、UIDatePicker日期擷取
0、介紹:UIDatePicker(及實現)
鬧鐘。。
格式固定,資料有規律,保證日期格式的正確
通常以UITextField的inputView的形式出現(一般不會單獨放一個UIDatePicker)
點擊文字框不再彈出的是系統鍵盤,而是一個時間選取器 (頂部還有一個功能條)
使用:
1)、初始化
2)、常用的設定
mode:4:只顯示time、date、time和date counter and timer 計時器類似
locale:本地化
textfield有一個inputView的屬性
self.textField.inputView =[[UIDatePicker alloc]init];
inputAccessoryView 設定鍵盤上面的工具列
1、擷取日期
NSDate *date =[_pickerview date];
2、日期轉字串並顯示日期
3、textField的inputView和inputAccessoryView屬性的使用
#pragma mark -考慮到直接在inputView屬性中,建立UIPickerView 就意味著每次點擊textField都會被重新建立 解決:使用控制項懶載入
//textfield 的inputView屬性
self.textField.inputView =self.pickerview;
//設定鍵盤上部的工具條
self.textField.inputAccessoryView =self.toolbar;
4、控制項懶載入
a、@property(nonatomic,strong) UIToolbar *toolbar;
注意使用strong強引用:為啥?
strong:不會被釋放
那為什麼平常寫控制項的時候,為什麼用weak?
因為在storyview中拖進來,意味著將控制項添加到了控制器中,相當於,控制器的view有個強指標指向這個控制項,所以此時,如果控制項也強指向控制器的話,不是無法釋放,而是需要釋放兩次。
那麼,為什麼用代碼建立的時候,也需要用weak:因為當控制項建立好之後,緊接著就會被添加到控制器的view中,也相當於,控制器對這個控制項有了一個強引用
b、
-(UIDatePicker *)pickerview
{
if(_pickerview ==nil)
{
//1、初始化
_pickerview =[[UIDatePicker alloc]init];
//2、本地化
_pickerview.locale =[NSLocale localeWithLocaleIdentifier:@"fr"];
//3、日期選取器的顯示模式
_pickerview.datePickerMode =UIDatePickerModeDate;
}
return _pickerview;
}
c、直接調用self.pickerview可擷取
5、新控制項toolbar的介紹及3種建立button 的方法
方式一、常規建立(initWithTitle)
UIBarButtonItem *cancle =[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancleClick)];
方式二、系統fiexibleSpacing按鈕(initWithBarButtonSystemItem)
//建立fiexibleSpacing系統按鈕
UIBarButtonItem *fiexibleSpacing =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
方式三、使用button形式外包itembutton的按鈕(initWithCustomView)
UIButton *donebtn =[[UIButton alloc]initWithFrame:CGRectMake(200, 0, 40, 40)];
[donebtn setTitle:@"確定" forState:UIControlStateNormal];
//[donebtn setTintColor:[UIColor blackColor]];
[donebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[donebtn addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *done = [[UIBarButtonItem alloc]initWithCustomView:donebtn];
6、鍵盤消失的兩種設定
鍵盤消失:
1、self.textField resignFirstsResponse
2、self.view endEditing :YES];(適用於多個需要設定鍵盤消失)
7、toolbar的一些注意點
五、點擊取消或完成需要執行的操作
UIToolBar的背景設定:Bar Tint 設定background是看不到的
item的圖片和文字不能同時設定
要想同時顯示文字和圖片,需要添加button
但是顏色與item的不一樣
方式一、Assets - 圖片 - Render AS
方式二、代碼修改(imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal)
例如:
建立帶圖片的button item
UIImage *img=[UIImage imageNamed:];
UIBarButtonItem *done =[UIBarButton Item alloc]initWithImage:img
會發現本來一個橙色的圖片變成系統色
img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
七、專案檔介紹
專案檔介紹
複習:UIPickerView/UIDatePicker
1、info.plist
2、pch檔案(xcode6之後消失,因為編譯太慢)
0)空項目可以通過 new - target -file建立
上一個檔案夾:顯示的是常用的檔案
1)assets.scassets - app icon:應用頭像
launch screen-啟動圖片
2)infoplist檔案
xcode6以前,名稱為“項目名稱-info.plist”
xml語言
Application Transport Security NSAllowsArbitraryLoads
2.1)bundle identifier:應用的唯一標示:如果兩個應用,使用同一個標示的話,就會被覆蓋(
應用程式升級也是通過判斷如果兩個bundleid一致的話,則直接覆蓋原有的程式
2.2)bundle(display name)
應用程式名稱
2.3)bundle version
應用程式的版本號碼(內部使用的版本號碼)
bundle visions string short:發布應用程式之後的版本號碼
2.4)mainstoryboard file base name :main
對應Main.storyboard:啟動之後為什麼會開啟mainstoryboadr的原因
2.5)supported interface orientations
手機的home button方向
2.6)擷取版本號碼:當更新時可顯示:應用程式當前更新了哪些功能的介紹
NSDictionary *infoDict=[NSBundle mainBundle].infoDictionary;(可以擷取到info.plist檔案 唯讀,可以列印看看)
三、pch檔案
1、建立:other-pct filepreFixHeader:先行編譯
2、作用:
a、幫我們把所有公用的宏放起來
[UIColor colorwithRed :arct4random_uniform(256)/255.0 green: arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0)];
把顏色直接在定義在prefixHeader.pch檔案中,定義宏: #define CZRandom
定義宏的時候 盡量將項目的首碼加上
b、配置
項目- targets -build settings - prefix header-debug(測試)/release(發布的時候)
每次發布前都要考慮prefix header檔案是否正確(便捷方式:點擊右邊 看prefixheader.pch檔案的路徑 “./”指向專案檔目錄 展開之後會發現,測試和發布是同一個pch檔案)
c、可以控制列印
#ifdef DEBUG
#define CZLog(…)NSLog(__VA_ARGS__)
#else
//發布版本
#define CZLog(…)
#denif
把NSLog寫成CZLog
如果是發布版本禁止列印
項目-TARGETS-EditSheme -Run -info -bulid configuration -release
d、儲存公用的標頭檔
如果一個工具類的標頭檔在這個類以及其它類中都有可能用到 每次都要包含那個標頭檔的解決:避免多處匯入
在pch檔案中:#import ”CZTool.h”
e、c語言類型的檔案
包含C語言檔案,編譯的時候會報錯,因為c語言中沒有oc之類的標頭檔
在pch檔案中需要屏蔽掉 oc寫的內容
#ifdef __OBJC__
#endif
Objective-c——UI進階開發第一天(UIPickerView和UIDatePicker)