Ios 文本字段不会成为第一响应者

Ios 文本字段不会成为第一响应者,ios,uialertcontroller,becomefirstresponder,Ios,Uialertcontroller,Becomefirstresponder,我似乎遇到了这样一种情况,调用BecomeFirstResponder()没有按预期工作 var alertView = new UIAlertView("Title", "Message", null, "Cancel", null); alertView.Show(); 假设视图中有两个文本框。第一个文本框是ViewDidLoad()的第一个响应程序 点击一个按钮,我希望第二个文本框成为第一个响应者 问题是如果我使用下面的行启动一个对话框 var alertView = UIAlertCo

我似乎遇到了这样一种情况,调用BecomeFirstResponder()没有按预期工作

var alertView = new UIAlertView("Title", "Message", null, "Cancel", null);
alertView.Show();
假设视图中有两个文本框。第一个文本框是ViewDidLoad()的第一个响应程序

点击一个按钮,我希望第二个文本框成为第一个响应者

问题是如果我使用下面的行启动一个对话框

var alertView = UIAlertController.Create("Title", "Message", UIAlertControllerStyle.Alert);
alertView.ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext;

var negativeAction = UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null);
alertView.AddAction(negativeAction);

//if (!string.IsNullOrWhiteSpace(""))
//{
//  var positiveAction = UIAlertAction.Create("", UIAlertActionStyle.Default, null);
//  alertView.AddAction(positiveAction);
//}

PresentViewController(alertView, true, delegate {
    alertView.ResignFirstResponder();
});
然后调用
tbSecond.BecomeFirstResponder()第一响应者无法更改

如果我使用旧的UIAlert类启动一个对话框,如下所示,第一响应程序将按预期工作

var alertView = new UIAlertView("Title", "Message", null, "Cancel", null);
alertView.Show();
然后打电话

tbSecond.BecomeFirstResponder();
如果我改变呼叫顺序,即呼叫tbSecond.BecomeFirstResponder(),我无法更改呼叫顺序;在调用ShowDialog之前,它会按预期工作。但是由于各种原因,我不能改变顺序。

你可以用这个

DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
    alertController.textFields?.last?.becomeFirstResponder()
}
你可以用这个

DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
    alertController.textFields?.last?.becomeFirstResponder()
}

如果您有两个文本字段,并且希望快速切换键盘,则需要在第一个文本字段上退出FirstResponder,并在第二个文本字段上成为FirstResponder。Afzal按照您的建议,将焦点从第一个文本框中移除,但第二个文本框仍然没有焦点。此外,如上所述,如果我使用UIAlertView,那么我不必辞职FirstResponder,因此我仍然想知道为什么我必须首先辞职UIAlertController。如果你有两个文本字段,并且你想快速切换键盘,那么你需要在第一个文本字段上辞职FirstResponder,然后在第二个文本字段上成为FirstResponder。Afzal,按照您的建议,将焦点从第一个文本框中移除,但是第二个文本框仍然没有焦点。此外,如上所述,如果我使用UIAlertView,那么我就不必辞职FirstResponder,所以我仍然想知道为什么我必须先辞职UIAlertController。