IOS開發——UI進階篇(八)pickerView簡單使用,通過storyboard載入控制器,註冊介面,通過xib建立控制器,控制器的view建立,導航控制器的基本使用

來源:互聯網
上載者:User

標籤:

一、pickerView簡單使用

1.UIPickerViewDataSource

這兩個方法必須實現

// 返回有多少列- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;// 返回第component有多少行- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

2.UIPickerViewDelegate

常用方法

// 返回第component列多寬- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component// 返回第component列多高- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component// 返回第component列第row列名- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component// NSAttributedString:富文本,可以描述文本的外觀屬性,顏色,字型,陰影,空心,圖文混排- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component// 返回第component列第row行視圖控制項- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view// 當使用者選中某一行的時候調用// 選中第component列第row行的時候調用// 可以監聽pickerView滾動- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

 

二、註冊介面

1.UITextFieldDelegate

有些時候要攔截使用者的輸入

只能通過PickerView輪動選擇來顯示文字框內容

那麼只要實現下面這個代理方法就行了

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    return NO;}

UITextFieldDelegate還有兩個常用方法

// 是否允許開始編輯// 允許編輯文字框- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField// 是否允許文字框結束編輯- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

 

2.自訂鍵盤
鍵盤由文字框inputView屬性決定

 

3.KVC底層實現

// setValuesForKeysWithDictionary底層實現//  利用KVC字典轉模型,    [flag setValuesForKeysWithDictionary:dict];    // 1.遍曆字典中的所有key    [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {        // 2.給模型的屬性賦值,利用KVC,把字典中的key當做模型的屬性名稱使用,字典中的值傳遞給模型的屬性.        [flag setValue:obj forKey:key];        // name -> icon        // KeyPath:模型中的屬性名稱        // 屬性的值//        [flag setValue:dict[@"name"] forKey:@"name"];//        [flag setValue:dict[@"icon"] forKey:@"icon"];    }];// setValue:forKey:底層實現// 給模型中的icon屬性賦值// [flag setValue:dict[@"icon"] forKey:@"icon"];// 1.首先去尋找模型中有木有setIcon:方法,直接調用setIcon:方法,[flag setIcon:dict[@"icon"]]// 2.接著尋找模型中有沒有icon的屬性名稱,如果有,就直接賦值 icon = dict[@"icon"]// 3.接著尋找模型中有沒有_icon的屬性名稱,如果有,就直接賦值 _icon = dict[@"icon"]// 4.找不到,直接報錯,setValue:forUndefinedKey:

 

4.UIDatePicker自訂生日鍵盤

// 自訂生日鍵盤- (void)setUp{        // 建立一個UIDatePicker    UIDatePicker *datePicker = [[UIDatePicker alloc] init];        // 設定日期模型    datePicker.datePickerMode = UIDatePickerModeDate;        // 設定地區,zh:中國    datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];        // 監聽UIDatePicker的選中的日期    [datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];        self.inputView = datePicker;}// 選中日期的時候調用- (void)dateChange:(UIDatePicker *)datePicker{//    NSLog(@"%@",datePicker.date);    // 擷取日期 顯示到文字框上        // 日期格式字串對象    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];        // 設定格式 1988-01-01    fmt.dateFormat = @"yyyy-MM-dd";            self.text = [fmt stringFromDate:datePicker.date];        }

 

三、通過storyboard載入控制器
 // UIStoryboard : 幫你載入storyboard檔案    // 載入storyboard檔案    // name:storyboard檔案名稱,不需要尾碼名    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    // 建立storyboard描述的控制器    // instantiateInitialViewController幫你載入箭頭指向的控制器    UIViewController *vc = [storyboard instantiateInitialViewController];    // 根據標識符建立storyboard描述的控制器//    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"org"];

 

四、通過xib建立控制器

1.通過xib建立控制器原因:
是想通過Xib描述控制器的view

2.如何通過xib建立控制器

1)讓xib與控制器產生聯絡,設定xib的檔案擁有者是控制器,這時候xib就描述這個控制器

2)連線,告訴控制器是哪個view在描述


五、控制器的view建立

1.loadView

什麼時候調用:當第一次使用控制器的view的時候就會調用

作用:載入控制器的view,自訂控制器的view

注意:

  1)只要重寫loadView,必須自己手動建立控制器的view

  2)在沒給_view賦值之前,不能調用self.view;


2.loadView載入流程


3.xib載入控制器的view

init底層會調用initWithNibName:bundle:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];        // 根據xib建立視窗的根控制器    // init底層會調用initWithNibName:bundle:    XMGViewController *vc = [[XMGViewController alloc] initWithNibName:nil bundle:nil];        // 通過xib建立XMGViewController控制器的view    // 1.判斷下nibName有沒有值,如果有值,就會去載入nibName指定的xib    // 2.如果nibName為空白,會先去尋找有沒有XMGView.xib,如果有就去載入    // 3.如果沒有XMGView.xib,就會去載入根類名同名的xib:XMGViewController.xib    // 4.如果還沒有找到,就產生一個空的view        self.window.rootViewController = vc;        [self.window makeKeyAndVisible];        return YES;}

 

4.控制器的view消極式載入

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    // 建立控制器,並不會建立控制器的view,前提是父類是UIViewController    ViewController *vc = [[ViewController alloc] init];        vc.view.backgroundColor = [UIColor redColor];// 控制器在這裡才用到(載入)view        // 設定視窗的根控制器    self.window.rootViewController = vc;        // 顯示視窗,拿到控制器的view添加到視窗上顯示    [self.window makeKeyAndVisible];    return YES;}

實際上就是一個懶載入

- (UIView *)view{    if (_view == nil) {        // 載入view        [self loadView];        // 當控制器的view載入完成的時候調用        [self viewDidLoad];    }    return _view;}

 

5.控制器view預設的是幾乎透明的

 

六、導航控制器的基本使用

1.導航控制器必須要有一個根控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        // 1.建立視窗    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];        // 建立一個導航控制器的根控制器    UIViewController *vc = [[OneViewController alloc] init];        // 建立導航控制器    // 導航控制器必須要有一個根控制器    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];          // 2.設定視窗的根控制器    self.window.rootViewController = nav;        // 3.顯示視窗    [self.window makeKeyAndVisible];            return YES;}

2.如果是導航控制器的子控制器可以直接拿到導航控制器

// 跳轉到第二個介面- (IBAction)jump2Two:(id)sender {        // 建立第二個控制器    TwoViewController *two = [[TwoViewController alloc] init];        // 跳轉    // 如果是導航控制器的子控制器可以直接拿到導航控制器    [self.navigationController pushViewController:two animated:YES];}

3.initWithRootViewController底層其實是調用導航控制器的push方法,把vc成為導航控制器的子控制器

 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];    // 會調用push方法    // initWithRootViewController底層其實是調用導航控制器的push方法,把vc成為導航控制器的子控制器//    [nav pushViewController:vc animated:YES];

 

IOS開發——UI進階篇(八)pickerView簡單使用,通過storyboard載入控制器,註冊介面,通過xib建立控制器,控制器的view建立,導航控制器的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.