简体   繁体   中英

UITextField resignFirstResponder not working?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM