UITextField resignFirstResponder 不起作用?
我已经仔细检查了 nib 文件中的所有连接。我的代码 -
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"iphone_bg_login.png"]];
self.title = @"Login screen";
loginTxt = [[UITextField alloc] init];
pwdText = [[UITextField alloc] init];
loginFailedTxt = [[UILabel alloc] init];
loginBtn = [[UIButton alloc] init];
navAppDelegate = (NavAppDelegate *)[[UIApplication sharedApplication] delegate];
navAppDelegate.navController.navigationBarHidden = YES;
//NSArray *subVs = (NSArray *) [self.view subviews];
[super viewDidLoad];
}
我使用了 UIView (UIControl)
的子类,并在界面构建器中向其中添加了所有 UI 元素。UIControl 的 touchDown
方法连接到 backgroundTap
方法。
-(IBAction) backgroundTap:(id) sender {
[loginTxt resignFirstResponder];
[pwdText resignFirstResponder];
//[[UIApplication sharedApplication] becomeFirstResponder];
//[sender resignFirstResponder];
}
所以键盘并没有像预期的那样被移除。不知道为什么。
感谢您的帮助! 特贾。
I've double checked all the connections in the nib file. My code -
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"iphone_bg_login.png"]];
self.title = @"Login screen";
loginTxt = [[UITextField alloc] init];
pwdText = [[UITextField alloc] init];
loginFailedTxt = [[UILabel alloc] init];
loginBtn = [[UIButton alloc] init];
navAppDelegate = (NavAppDelegate *)[[UIApplication sharedApplication] delegate];
navAppDelegate.navController.navigationBarHidden = YES;
//NSArray *subVs = (NSArray *) [self.view subviews];
[super viewDidLoad];
}
I've used a subclass of UIView (UIControl)
and added all the UI elements to it in the Interface builder.The UIControl's touchDown
method is connected to backgroundTap
method.
-(IBAction) backgroundTap:(id) sender {
[loginTxt resignFirstResponder];
[pwdText resignFirstResponder];
//[[UIApplication sharedApplication] becomeFirstResponder];
//[sender resignFirstResponder];
}
So the keyboard isn't removed like it's supposed to. Not sure why.
Thanks for the help!
Teja.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DyingCactus 已指出您的错误。您正在用完全不同的控件替换 NIB 版本的控件,从而丢失指向 NIB 中的指针。当您调用
resignFirstResponder
时,您是在重复的对象上调用它,而不是实际在屏幕上的对象。摆脱对 NIB 中连接的事物的alloc
和init
调用。DyingCactus has pointed to your error. You're replacing the NIB-version of the control with a completely different control, losing your pointer to the one in the NIB. When you call
resignFirstResponder
, you're calling it on your duplicate object, not the one that's actually on the screen. Get rid of thealloc
andinit
calls for things wired in the NIB.