这对我来说非常有用,它可以使键盘立即显示出来,而没有动画或延迟。
让textField
是一个实例变量MyViewController
(的子类UIViewController
)。
通话[textField becomeFirstResponder]
中initWithNibName:bundle:
(用于一个子类UIViewController
)或initWithStyle:
(对于的子类UITableViewController
),而不是在viewDidLoad
。例如:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[textField becomeFirstResponder];
}
return self;
}
或者,在初始化之后但在按下之前调用它UIViewController
。例如:
MyViewController *viewController = [[MyViewController alloc] init];
[viewController.textField becomeFirstResponder];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];