2011-06-11 43 views
0

我在cellForRowAtIndexPath方法中將UITextFields添加到UITableView。 一旦這一切都建立了,我需要訪問它們,例如我要讓鍵盤消失,當選擇的背景,如:如何引用在方法中創建的UITextField?

- (IBAction)touchBackground:(id)sender { 
    [textField resignFirstResponder]; 
} 

,並滾動到他們時,他們選擇了像:

- (void) textFieldDidBeginEditing:(UITextField *)textField { 
    UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview]; 
    [settingsTable scrollToRowAtIndexPath:[settingsTable indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
} 

但是,它們只存在於方法中的臨時變量,所以我不知道如何引用它們。我可以事先在頭文件中聲明它們,但是我希望我不必這樣做,因爲這種添加更多字段的方式改變了我所要做的就是添加到單元格標題數組中。

回答

3

您應該添加一個實例變量與此propoerty:

@property (assign) UITextField *selectredTextField; 

那麼你會選擇字段分配給這個實例變量

- (void) textFieldDidBeginEditing:(UITextField *)textField { 
    UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview]; 
    [settingsTable scrollToRowAtIndexPath:[settingsTable indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
    self.selectredTextField = textField; 
} 

現在在你的方法,你可以辭職,但

- (IBAction)touchBackground:(id)sender { 
    [self.selectredTextField resignFirstResponder]; 
} 
+0

太棒了。我有很多東西需要學習Objective-C和OOP。 – espekia 2011-06-11 19:36:45

相關問題