简体   繁体   English

如何在不同父视图中的文本字段之间切换焦点(becomeFirstResponder)

[英]How to switch focus (becomeFirstResponder) between textfields in different parent views

I have 3 custom objects which each contains 3 uitextfields. 我有3个自定义对象,每个对象包含3个uitextfields。

when I press next in the keyboard I am able to pass focus to the next UITextFiled though textFieldShouldReturn and becomeFirstResponder. 当我在键盘上按next时,我可以通过textFieldShouldReturn并成为FirstResponder将焦点传递给下一个UITextFiled。 this is fine while they reside in the same parent view. 当它们位于同一父视图中时,这很好。 But how can I pass focus to an UITextFiled in a different parent view? 但是,如何在其他父视图中将焦点传递给UITextFiled?

行是不同的视图

Each row represents a different view. 每行代表一个不同的视图。 So my problem is passing focus from text3 to text4. 所以我的问题是将焦点从text3传递到text4。

You'll want to use becomeFirstResponder on "text4". 您将要在“ text4”上使用becomeFirstResponder So, depending on how you have this all setup (I'll assume they're just IBOutlets ) you'll want to do something along the lines of this: 因此,根据您的所有设置方式(我假设它们只是IBOutlets ),您将需要执行以下操作:

- (IBAction) userHitReturnOnTextField:(UITextField *)sender {
     if (sender == self.text1) [self.text2 becomeFirstResponder];
     else if (sender == self.text2) [self.text3 becomeFirstResponder];
     ...
     else [self.view endEditing:YES]; // hides the keyboard
}

Of course you can get more creative and store your UITextFields in an array and your logic will get simpler depending on the number of textfields you have: 当然,您可以提高创造力,并将UITextFields存储在数组中,并且逻辑将变得更简单,具体取决于您拥有的文本字段的数量:

- (IBAction) userHitReturnOnTextField:(UITextField *)sender {
     int nextIndex = [self.myArrayOfTextFields indexOfObject:sender] + 1;
     if (nextIndex < self.myArrayOfTextFields.count) {
         UITextField *nextField = self.myArrayOfTextFields[nextIndex];
         [nextField becomeFirstResponder];
     } else [self.view endEditing:YES]; // hides the keyboard
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM