UITextField resignFirstResponder 不起作用?

发布于 2024-08-29 05:01:33 字数 1171 浏览 0 评论 0原文

我已经仔细检查了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2024-09-05 05:01:33

DyingCactus 已指出您的错误。您正在用完全不同的控件替换 NIB 版本的控件,从而丢失指向 NIB 中的指针。当您调用 resignFirstResponder 时,您是在重复的对象上调用它,而不是实际在屏幕上的对象。摆脱对 NIB 中连接的事物的 allocinit 调用。

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 the alloc and init calls for things wired in the NIB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文