UIAlertView keyboard doesn't get shown unless app is re-opened

。_饼干妹妹 提交于 2019-12-25 05:10:02

问题


When the user opens my app, I want to check for a value stored in the defaults, and if it is not present prompt the user to input a value. The storing/reading of the value appears to be working.

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"Checking if there is an email address set");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString * emailAddress = [defaults objectForKey:@"emailAddress"];
    if (IsEmpty(emailAddress))
    {
        NSLog(@"email address is blank, prompting user to enter one..");
        self.emailPromptAlert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                          message:@"Enter Email address:" 
                                                         delegate:self 
                                                cancelButtonTitle:@"Cancel" 
                                                otherButtonTitles:@"OK", nil];
        [self.emailPromptAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [self.emailPromptAlert setTag:1];
        [self.emailPromptAlert show];
    }
}

The problem that I have, is when I do a clean install of my app and load for the first time, the alert shows up as expected but there is no keyboard shown, so the user can't actually type anything.

Clicking the home button then bringing the app back into the foreground again, the same alert is on screen but this time the keyboard is actually showing.

What can I do to make sure the keyboard gets shown the first time?


回答1:


I am not totally sure, but maybe this could work?

self.emailPromptAlert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                      message:@"Enter Email address:" 
                                                     delegate:self 
                                            cancelButtonTitle:@"Cancel" 
                                            otherButtonTitles:@"OK", nil];
[self.emailPromptAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[self.emailPromptAlert setTag:1];

[self.emailPromptAlert show];

//try this?
UITextField *textField = [self.emailPromptAlert textFieldAtIndex:0];
[textField becomeFirstResponder];

This doesn't really get to the root of the problem of why it isn't showing in the first place, but it could be a solution.




回答2:


That's a weird behavior, Why don't you try showing the alert in the next runloop

[self.emailPromptAlert performSelector:@selector(show) withObject:nil afterDelay:0];



回答3:


I had the same problem. In my case, the keyboard was already visible at the time that I showed the alert. I resolved it by dismissing the keyboard before showing the alert:

[myTextField resignFirstResponder];
[myAlert show];


来源:https://stackoverflow.com/questions/11563461/uialertview-keyboard-doesnt-get-shown-unless-app-is-re-opened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!