UITextField 不会 return 并且文本字段无法更改。我该如何解决?

UITextField won't return and text field can't be changed. How do I fix this?

我在尝试让键盘转到下一个字段时遇到问题,或者 return 当按下 return 键时,我的代码是:

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (weak, nonatomic) IBOutlet UITextField *userField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (strong, nonatomic) UITextField *textField;
@end

@implementation LoginViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)viewDidLayoutSubviews
{
    [self.userField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)tapLogin:(id)sender {

    NSLog(@"login");
}


#pragma mark - UITextFieldDelegate methods

// for return and next
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.userField) {

    [self.passwordField becomeFirstResponder];
}
else if (textField == self.passwordField) {

    [self tapLogin:nil];
}
return YES;
}

光标在 userField 中,但按下 return 键什么也没做,如果我手动点击另一个字段,即密码,这也不会做任何事情。光标停留在用户名字段中。我已经在我的头文件中实现了该协议,并且还在界面构建器中将文本字段设置为委托。有人可以帮忙吗?

试试这个,

viewDidLayoutSubviews 中删除 [self.userField becomeFirstResponder]; 方法,即删除下面的方法

- (void)viewDidLayoutSubviews
{
    [self.userField becomeFirstResponder];
}

并添加

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [self.userField becomeFirstResponder];
}