简体   繁体   English

UITextField不辞职第一响应者?

[英]UITextField not resigning first responder?

I am presenting a view controller as a modal. 我将视图控制器呈现为模式。 This view controller has several UITextFields connected via IB. 该视图控制器具有几个通过IB连接的UITextField。

My issue is when hitting the return key, the delegate method is called, but for some reason the call to resignFirstResponder on the given textField is not. 我的问题是,当按下返回键时,会调用委托方法,但由于某种原因,在给定textField上调用resignFirstResponder的方法却没有。 Even if I print a message to the console in the textFieldShouldReturn delegate method it gets printed, but it will simply refuse to call the resign method. 即使我在textFieldShouldReturn委托方法中将消息打印到控制台,它也会被打印出来,但它只会拒绝调用resign方法。

In the shouldReturn delegate method I only have: 在shouldReturn委托方法中,我只有:

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return  YES;
}

This has really never happened to me before, it just simply will not resignFirstResponder. 我以前从未真正发生过这种情况,只是根本不会辞职FirstResponder。 As I mentioned before, I can place a print to console, just to verify that the shouldReturn method gets called and it does. 如前所述,我可以将打印品放置到控制台上,只是为了验证shouldReturn方法是否被调用并且可以调用。

I also tried this just to see if the delegate was been set or not and it sure made the change to the UITextField behavior. 我还尝试了一下,只是看是否设置了委托,并且确定对UITextField行为进行了更改。

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    [textField setTextAlignment:UITextAlignmentCenter];
}

This issue truly baffles me. 这个问题确实使我感到困惑。 Even when I attempt to call resignFirstResponder on the textfield directly it won't dismiss the keyboard. 即使当我尝试直接在文本字段上调用resignFirstResponder时,它也不会关闭键盘。

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([textField isEqual:self.productTextField]) {
        NSLog(@"Product textfield");
        [self.productTextField resignFirstResponder];
    } else if ([textField isEqual:self.quantityTextField]) {
        NSLog(@"Quantity textfield");
    }
    return YES;
}

Both logs work with their respective textfields and print their corresponding messages each. 这两个日志都使用各自的文本字段,并分别打印相应的消息。 But calling the method still does nothing when messaging the textfield directly. 但是,直接向文本字段传递消息时,调用该方法仍然无济于事。

Found the answer, apparently, UIPresentationFormSheet does not allow you to get rid of the keyboard. 找到了答案,显然,UIPresentationFormSheet不允许您摆脱键盘。 I tried with a different presentation style and it worked. 我尝试了一种不同的演示风格,并且奏效了。 @Cal, thanks for your question . @Cal,谢谢您的提问

I think you need to explicitely write [textField resignFirstResponder]; 我认为您需要明确编写[textField resignFirstResponder]; within your textFieldShouldReturn method. 在您的textFieldShouldReturn方法中。

If you do that already, maybe it's definitely weird. 如果您已经这样做了,也许绝对很奇怪。 Can you post your code? 您可以发布代码吗? :) :)

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

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