1

我有一个UIScrollView包含许多UITextField控件的视图。如果在输入任何UItextField控件后,我单击其他位置然后导航到另一个视图,一旦我返回相同的视图,我就无法再输入UITextField.

我也为UITextField视图上的每个控件提供了此代码。

//self.QuestionAnswer = UITextField - This is the control with the issues.
[self.QuestionAnswer addTarget:self action:@selector(SaveQuestionAnswer:) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.QuestionAnswer addTarget:self action:@selector(SaveQuestionAnswer:) forControlEvents:UIControlEventEditingDidEnd];


//sender = self.QuestionAnswer - This control is of type UITextField.
-(IBAction)SaveQuestionAnswer:(id)sender
//some code here
   [control resignFirstResponder];
//some code here
}

我还验证了[control resignFirstResponder]当失去对UITextField.

4

1 回答 1

0

我已经解决了我的问题。我所有的控制器都继承自一个基本控制器。在这个基本控制器中,我有一些方法可以处理我的应用程序的所有导航。

将这两种方法添加到我的基类中。如果您没有基类,则可以将其添加到您的控制器中。这将循环浏览视图上的所有控件并释放第一响应者。

-(void)StartReleaseAllFirstResponders
{
    for(UIView *Item in [self.view subviews]) {
        [self ReleaseAllFirstResponders:Item];
    }
}

-(void)ReleaseAllFirstResponders:(UIView*)OriginItem
{
    for(UIView *Item in [OriginItem subviews]) {
        [Item resignFirstResponder];
    }
    [OriginItem resignFirstResponder];
}

这是实现的一个例子。

-(void)Navigate_Home
{
    [self StartReleaseAllFirstResponders]; //Making sure we release all first responders
    HomeController *GoToView= [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"];
    GoToView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:GoToView animated:YES completion:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
于 2012-12-08T17:38:58.017 回答