按下键盘回车键时的操作(iOS)

13
- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{
    [self checkRun:nil];
    return YES;
}

我正在尝试使用上面的代码,在按下回车键时完成 IBAction checkRun,但似乎无效。我错在哪里了?我想也许是因为我没有直接引用我正在输入的文本字段,但我不知道需要在哪里放置该文本字段的名称。

提前致谢。


2
你设置了文本字段的“delegate”吗?你知道上述方法是否被调用了吗? - rmaddy
1
哦,我还没有!非常感谢你! - Nathan
2个回答

15

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textField.delegate = self;
}

2
没错,谢谢。但是为了参考,我只需在IB中将“delegate”拖到视图控制器中就可以了。可能不是很好的做法,但它起作用了。 - Nathan
4
在IB中将委托拖到视图控制器中实际上是最佳实践。 - Uzaak

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接