【问题标题】:UIAlertView choice causing resignFirstResponder to failUIAlertView 选择导致 resignFirstResponder 失败
【发布时间】:2011-06-08 18:37:35
【问题描述】:

我遇到了与 Anthony Chan's question 类似的问题,在尝试了所有建议的解决方案后,我仍然卡住了。

不知何故,只有在与我的 UIAlertView 交互后,我才能在我的应用程序的另一个视图中关闭键盘。就好像警报正在破坏我的 UITextField 的 resignFirstResponder 能力。下面我实例化我的 UIAlertView,然后调用它的 didDismissWIthButtonIndex 方法。然后,我调用 showInfo 方法,该方法加载另一个 UIViewController。

UIAlertView *emailFailAlert = [[UIAlertView alloc] initWithTitle:@"Error"
                            message:@"error message text."
                           delegate:self
                  cancelButtonTitle:@"Not now"
                  otherButtonTitles:@"Settings", nil];
[emailFailAlert setTag:2];
[emailFailAlert show];
[emailFailAlert release];

按下“设置”选项后,我将调用此方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {   
    if ([alertView tag] == 2) {    
        if (buttonIndex == 1){      
            [self showInfo:nil];
        }
    }   
}

我的 showInfo 方法通过以下代码加载另一个 ViewController:

- (IBAction)showInfo:(id)sender {
    FlipsideViewController *fscontroller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
    fscontroller.delegate = self;
    fscontroller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:fscontroller animated:YES];
    [fscontroller release];
}

单击此 Flipside VC 中的任何文本字段后,我无法像平常使用 - (BOOL)textFieldShouldReturn:(UITextField *)textField[textField resignFirstResponder] 那样关闭键盘。我已经省略了这段代码,因为这个问题很长,但如果有必要我很乐意发布。

有趣的是,如果我在单击按钮时注释掉 [self showInfo:nil] 调用并通过单击测试按钮(在 alertView didDismissWithButtonIndex: 方法之外)调用它,一切正常。知道这里发生了什么吗?

提前致谢!

【问题讨论】:

  • 更新:将 [self showInfo:nil] 更改为 [self performSelector:@selector(showInfo:) withObject:nil afterDelay:.1] 有效,但似乎这可能会在较慢的设备上中断(或在模拟器之外)......感觉就像一个奇怪的范围问题。任何帮助表示赞赏!
  • 遇到了您描述的相同问题。出于某种原因,如果您安排模态演示(或在我的情况下关闭)在下一个运行循环中运行,那么问题就会消失 - 键盘不会作为第一响应者卡住。谢谢!
  • 我还要注意 - 这个问题似乎只影响 iOS4。我在 iOS5+ 中没有看到这个问题。

标签: iphone ios keyboard uitextfield uialertview


【解决方案1】:

当警告,具有多个解除选项,在键盘上方被调用 - 键盘变为不可解除,活动文本字段上的 resignFirstResponder ;

在显示警报之前,您需要关闭键盘。

假设你的 UITextField 被称为 myTextField;

[myTextField resignFirstResponder]; //That's the only line I added

UIAlertView *emailFailAlert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                         message:@"error message text."
                                                        delegate:self
                                               cancelButtonTitle:@"Not now"
                                               otherButtonTitles:@"Settings", nil];
[emailFailAlert setTag:2];
[emailFailAlert show];
[emailFailAlert release];

我希望这对那些不得不处理这个奇怪的晦涩问题的人有所帮助。

【讨论】:

  • 我对这个失去了希望!谢谢!
【解决方案2】:

您不应直接致电alertView:didDismissWithButtonIndex:。在警报消失后,此委托方法将在所有情况下自动执行。否则代码会运行两次!

【讨论】:

  • 谢谢,但我没有直接打电话给它;当用户点击 UIAlertView 中的一个按钮时,它会被调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 2013-12-10
  • 2017-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多