IOS學習筆記(六)inputAccessoryView,inputView


我們在使用UITextView和UITextField的時候,可以通過它們的inputAccessoryView屬性給輸入時呼出的鍵盤加一個附屬視圖,通常是UIToolBar,用於回收鍵盤。

但是當我們要操作的視圖不是UITextView或UITextField的時候,inputAccessoryView就變成了readonly的。

這時我們如果還想再加inputAccessoryView,按API中的說法,就需要新建一個該視圖的子類,並重新聲明inputAccessoryView屬性為readwrite的。比如我們要實現點擊一個tableView的一行時,呼出一個UIPickerView,並且附加一個用於回收PickerView的toolbar。因此我們自建一個UITableViewCell類,並聲明inputAccessoryView和inputView為readwrite的,並且重寫它們的get方法,這樣在某個tableviewcell變成第一響應者時,它就會自動呼出inputView和inputAccessoryView;

1 @interface MyTableViewCell : UITableViewCell<UIPickerViewDelegate,UIPickerViewDataSource>
2 {
3     UIToolbar *_inputAccessoryView;
4     UIPickerView *_inputView;
5 }
6 @property(strong,nonatomic,readwrite) UIToolbar *inputAccessoryView;
7 @property(strong,nonatomic,readwrite) UIPickerView *inputView;
8 @end

.m中的get方法:

 1 -(UIToolbar *)inputAccessoryView
 2 {
 3     if(!_inputAccessoryView)
 4     {
 5         UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
 6 //        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItem target:self action:@selector(dodo)];
 7         UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dodo)];
 8         toolBar.items = [NSArray arrayWithObject:right];
 9         return toolBar;
10     }
11     return _inputAccessoryView;
12 }
13 -(UIPickerView *)inputView
14 {
15     if(!_inputView)
16     {
17       UIPickerView *  pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 200, 320, 200)];
18         pickView.delegate =self;
19         pickView.dataSource = self;
20         pickView.showsSelectionIndicator = YES;
21         return pickView;
22     }
23     return _inputView;
24 }
25 -(void)dodo
26 {
27     [self resignFirstResponder];
28 }
29 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
30 {
31     return 1;
32 }
33 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
34 {
35     return [NSString stringWithFormat:@"%d",row];
36 }
37 // returns the # of rows in each component..
38 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
39 {
40     return 5;
41 }

但是這時運行后還是沒有反應,最后在一個網頁中查到這樣的話:

What is the best, we aren't limited to use this feature on UITextFields only. Because of fact that UIView inherits from UIResponder, we can attach this behaviour to all views, for example to a button or a table cell. To do that we have to override canBecomeFirstRespondermethod in our subclass. For example, the UIButton subclass implementation can look like this:

    implementation CustomButton { } //it is UIButton subclass
    
    @synthesize inputView, inputAccessoryView;
    
    - (BOOL) canBecomeFirstResponder {
        return YES;
    }
    
    - (void)dealloc {
        [inputView release];
        [inputAccessoryView release];
        [super dealloc];
    }
    
    @end
因此我在.m中重寫
canBecomeFirstResponder方法
-(BOOL)canBecomeFirstResponder
{
    return YES;
}

但是這時運行是還是沒有反應,最后我只好在代碼中當cell被選中時,手動把它變成第一響應者。(難道cell被選中時不是第一響應者?)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell becomeFirstResponder];
}

運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM