ResignFirstResponder doesn't dismiss the keyboard (iPhone)

后端 未结 6 1017
情话喂你
情话喂你 2020-12-16 04:06

I\'ve searched through this site that I couldn\'t find a solution to the problem I\'m facing now. Hope someone can help.

I\'ve created a UIAlertView to prompt user

6条回答
  •  粉色の甜心
    2020-12-16 04:58

    First you need to define your enterNameAlert in interface header. then use the below code.

    - (void)viewDidLoad {    
        [super viewDidLoad];
        enterNameAlert = [[UIAlertView alloc] initWithTitle:@"Enter your name"
                                                                 message:@"\n\n\n"
                                                                delegate:self
                                                       cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
                                                       otherButtonTitles:NSLocalizedString(@"OK", nil),nil];
    
        UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
        enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert;
        enterNameField.borderStyle = UITextBorderStyleRoundedRect;
        enterNameField.autocorrectionType = UITextAutocorrectionTypeNo;
        enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing;
        enterNameField.returnKeyType = UIReturnKeyDone;
        enterNameField.delegate = self;    
        [enterNameAlert addSubview:enterNameField];
        [enterNameField becomeFirstResponder];
        [enterNameAlert show];    
        [enterNameField release];
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        if ([textField isFirstResponder]) {
            [textField resignFirstResponder];
            [enterNameAlert dismissWithClickedButtonIndex:1 animated:YES];
            [enterNameAlert release];
            NSLog(@"text field was first responder");
        } else {
            [textField becomeFirstResponder];
            [textField resignFirstResponder];
            NSLog(@"text field was not first responder");
        }
        NSLog(@"textfieldshouldreturn");
        return YES;
    }
    

提交回复
热议问题