1、如何監聽控件的一些事件 或者 行爲 * 若是父類是UIControl,說明是經過addTarget:action:forControlEvents:方法來監聽控件的一些事件 * 若是父類不是UIControl,說明通常是經過代理方法來監聽控件的一些行爲 2、UIDatePicker 1.常見屬性 /* 樣式 UIDatePickerModeTime,時間 UIDatePickerModeDate,日期 UIDatePickerModeDateAndTime 日期 + 時間 */ @property(nonatomic) UIDatePickerMode datePickerMode; /* 區域 中國:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] */ @property(nonatomic,retain) NSLocale *locale; 2.事件監聽 1> 由於它繼承自UIControl,因此跟按鈕同樣監聽 - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; 2> 事件類型:UIControlEventValueChanged 3、UIPickerView 1.須要靠dataSource和delegate來顯示數據 @property(nonatomic,assign) id<UIPickerViewDataSource> dataSource; @property(nonatomic,assign) id<UIPickerViewDelegate> delegate; 2.常見數據源和代理方法 1> 數據源方法 // 一共有多少列 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; // 第component列有多少行 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 2> 代理方法 // 第component列第row行顯示怎樣的文字 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; // 第component列第row行顯示怎樣的view - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; // 選中了第component列第row行就會調用 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; 3.常見方法 1> 是否顯示「選中指示器」 @property(nonatomic) BOOL showsSelectionIndicator; 2> 刷新數據(從新調用數據源和代理的方法來顯示數據) - (void)reloadAllComponents; // 刷新全部的列 - (void)reloadComponent:(NSInteger)component; // 只刷新第component列 3> 經過代碼選中第component列第row行 - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; 4> 得到第component列所選中的行號 - (NSInteger)selectedRowInComponent:(NSInteger)component; 4、UITextField 1.常見屬性 1> 鍵盤 @property (readwrite, retain) UIView *inputView; 2> 鍵盤頂部的工具條 @property (readwrite, retain) UIView *inputAccessoryView; 3> 代理 @property(nonatomic,assign) id<UITextFieldDelegate> delegate; 2.常見方法 1> 叫出鍵盤 - (BOOL)becomeFirstResponder; 2> 退出鍵盤 - (BOOL)resignFirstResponder; 3.常見代理方法 1> 若是返回NO,表明文本框不能編輯、不能彈出鍵盤 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; 2> 若是返回NO,表明禁止改變文本框的文字(不能增長、刪除文字) - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 3> 點擊了鍵盤右下角的按鈕就會調用(return key) - (BOOL)textFieldShouldReturn:(UITextField *)textField; 5、UIButton的狀態 UIControlStateNormal // 默認、普通 UIControlStateHighlighted // 高亮(當用戶長按的時候達到這種狀態) UIControlStateDisabled // 不可用(這種狀態下的按鈕不能處理任何點擊事件,enabled = NO的時候就能達到這種狀態) // 當切換狀態的時候,按鈕就會顯示對應狀態的背景圖片、小圖片、文字、文字顏色 6、控件的封裝 1.爲何封裝? 1> 重用某個經常使用的功能 2> 屏蔽某個功能的實現細節 2.封裝的步驟 1> 自定義一個View(新建一個繼承UIView的類) 2> 若是控件內部的內容是固定的,能夠用一個xib文件來描述所封裝控件內部的細節 3> 所封裝控件內部的事件,應該經過代理傳遞出去 * 當所封裝控件內部發生了一些事情,應該通知代理,代理得知內部的事件後,就能夠在代理方法中實現想作的事情
1、UITextField的代理方法 #pragma mark 當文本框開始編輯的時候調用---開始聚焦 - (void)textFieldDidBeginEditing:(UITextField *)textField 2、排序 1.可變數組的排序(NSMutableArray) * sortUsingComparator:方法調完,會直接改變array這個可變數組內部對象的順序 [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { }]; * 排序過程當中會不斷地調用block,傳入兩個須要比較的對象:id obj1, id obj2 * block必須有返回值:NSComparisonResult * NSComparisonResult有3種取值: NSOrderedAscending = -1L, // 右邊的對象排後面 NSOrderedSame, // 同樣 NSOrderedDescending // 左邊的對象排後面 2.不可變數組的排序(NSArray) * sortedArrayUsingComparator:方法並不會改變array數組內部的順序 * sortedArrayUsingComparator:方法會返回一個新的已經排好序的數組sortedArray NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { }]; 3、監聽鍵盤的顯示和隱藏 1.監聽鍵盤通知 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; // 1.顯示鍵盤 [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // 2.隱藏鍵盤 [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 2.移除鍵盤通知(非ARC必須寫) - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } 3.注意點:當彈出一個新的鍵盤時,纔會發出顯示鍵盤的通知
1、UITextField的代理方法 #pragma mark 當文本框開始編輯的時候調用---開始聚焦 - (void)textFieldDidBeginEditing:(UITextField *)textField 2、排序 1.可變數組的排序(NSMutableArray) * sortUsingComparator:方法調完,會直接改變array這個可變數組內部對象的順序 [array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { }]; * 排序過程當中會不斷地調用block,傳入兩個須要比較的對象:id obj1, id obj2 * block必須有返回值:NSComparisonResult * NSComparisonResult有3種取值: NSOrderedAscending = -1L, // 右邊的對象排後面 NSOrderedSame, // 同樣 NSOrderedDescending // 左邊的對象排後面 2.不可變數組的排序(NSArray) * sortedArrayUsingComparator:方法並不會改變array數組內部的順序 * sortedArrayUsingComparator:方法會返回一個新的已經排好序的數組sortedArray NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { }]; 3、監聽鍵盤的顯示和隱藏 1.監聽鍵盤通知 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; // 1.顯示鍵盤 [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // 2.隱藏鍵盤 [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 2.移除鍵盤通知(非ARC必須寫) - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } 3.注意點:當彈出一個新的鍵盤時,纔會發出顯示鍵盤的通知