2011-11-25 136 views
3

我正在一個應用程序的tableViewtextField在其每個單元格(有20多個單元格)右側。 我已經爲每行創建了自定義單元格,除了最後一個。 在最後一行中只有一個按鈕。隱藏鍵盤/ resignFirstResponder強制

現在我想打電話resignFirstResponder按鈕的點擊。
我該怎麼辦請幫忙?

+0

所以,在文本框的點擊UR越來越K/B和U想辭職日ķ/b在該按鈕上單擊。 – mAc

+0

+1爲好問:) – mAc

回答

2

您將不得不跟蹤哪個單元格具有第一個響應者的文本字段,並像這樣將其辭職。

[myCellTextField resignFirstResponder]; 
2

您可能想要用鍵盤跟蹤文本字段。在控制器中實現<UITextFieldDelegate>協議,並將控制器設置爲每個文本字段的委託。寫textFieldDidBeginEditing:方法像這樣,設置所謂currentTextField實例變量:

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    currentTextField = [textField retain]; 
} 

然後,在你的行動按鈕運行[currentTextField resignFirstResponder]

1

Aopsfan的答案可能是迄今爲止最好的解決方案。然而,要添加到它(我不能發表評論),千萬記得要解除分配對象:

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    if (currentTextField != nil) { 
     [currentTextField release]; 
    } 
    currentTextField = [textField retain]; 
} 

更妙的是使用@屬性的和@synthesize所以運行時可以爲你做的內存管理。

[視圖控制器] .H

@property (nonatomic, retain) UITextField* currentTextField; 

[視圖控制器] .M

@synthesize currentTextField = _currentTextField; 

- (void)viewDidLoad|Appear { 
    self.currentTextField = nil; 
} 

- (void) dealloc { 
    [_currentTextField release], _currentTextField = nil; 
    ... 
    [super dealloc]; 
} 

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    self.currentTextField = textField; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (self.currentTextField) { 
     [self.currentTextField resignFirstResponder]; 
    } 
}