AlertView在解雇后触发键盘

时间:2015-05-08 23:19:38

标签: ios objective-c uialertview

所以这种情况正在发生。

我有一个视图控制器,它处理我的应用程序中的所有警报视图。我有另一个视图控制器,它有一个用户可以编辑的UITextView和一个保存按钮。

当他们点击保存按钮时,如果文本已经保存,它会触发一个警告,询问他们是否确定要更新,如果他们确认,则更新文件并给他们第二个警告说这是成功的。

当发生第二个警报时,键盘会一直弹出。我已经尝试重新启动键盘,并在按下保存按钮后立即关闭文本字段上的用户交互启用标志。

self.storyEditorTextView.userInteractionEnabled=NO;
[self.storyEditorTextView resignFirstResponder];

我还尝试在响应警报时将其关闭(因为某些警报可能有文本字段)。

更糟糕的是,当我注释掉resign和userInteractionsEnabled行(包括警报中的那一行)时,键盘仍会在第一个警报被解除后出现,当第二个警报被解除时消失(如果你可以点击它,因为键盘覆盖它),你无法进入UITextView并调出键盘而无需回到父视图。

这是警报代码。

- (void)addPromptToFavorites
{
// throw up an alert to confirm and get a name
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Name Your Favorite!" 
                                                        message:@"Would you like to add a name to your prompt?"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"OK",nil];
        // add a text field
        alert.alertViewStyle = UIAlertViewStylePlainTextInput;
        UITextField *textField = [alert textFieldAtIndex:0];
        textField.text = @"My Great Prompt";

        // set the tag
        [alert setTag:SAVE_FAVE];

        // Display the alert to the user
        [alert show];
}


- (void)updateFave: (NSNumber *) theFaveId
{
    NSLog(@"UPDATE FAVE\n\nself.sharedFaveMan.tempFave %@",self.sharedFaveMan.tempFave);
   // NSMutableDictionary *faveDict =[[NSMutableDictionary alloc] init];

    // loop through the favoritePrompts array until you find a match to the faveID
    for (id element in self.sharedFaveMan.favoritePrompts) {
        NSNumber *valueForID = [element valueForKey:@"id"];

        if([valueForID isEqualToNumber:theFaveId])
        {
            self.sharedFaveMan.tempFave=element;
        }
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Update Your Favorite!"
                                                    message:@"The story will be saved with the currently selected Favorite."
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"OK",nil];

    //set the tag
    [alert setTag:UPDATE_FAVE];

    // Display the alert to the user
    [alert show];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    UITextField *textField = [alertView textFieldAtIndex:0];

    [textField resignFirstResponder];

    if (buttonIndex !=0)
    {
      if(alertView.tag==UPDATE_FAVE)
        {
            NSLog(@"Updating Fave");
            // loop through the favoritePrompts array until you find a match to the faveID
            int counter=0;
            for (id element in self.sharedFaveMan.favoritePrompts) {
                NSNumber *valueForID = [element valueForKey:@"id"];

                if([valueForID isEqualToNumber:self.sharedFaveMan.theFaveID]){
                    break;
                }
                counter ++;
            }


            // update the pieces of the prompt
            [[self.sharedFaveMan.favoritePrompts objectAtIndex:counter] setObject:self.sharedFaveMan.faveStoryText forKey:@"storyText"];

            // save it
            [self saveFavorites];

            [[NSNotificationCenter defaultCenter]
             postNotificationName:@"updateTheTable"
             object:self];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success!"
                                                            message:@"Favorite Updated!"
                                                           delegate:self
                                                  cancelButtonTitle:nil
                                                  otherButtonTitles:@"OK",nil];

            //play a sound
            [self createSoundID: @"ticktock.aiff"];

            //set the tag
            [alert setTag:UPDATE_COMPLETE];

            // Display the alert to the user
            [alert show];

        }
    }
    else if (buttonIndex == 0)
    {
        NSLog(@"%ld",(long)alertView.tag);

        if(alertView.tag==SAVE_FAVE)
        {
            // they canceled the save

        }
        else if (alertView.tag==UPDATE_COMPLETE)
        {
            NSLog(@"Hit that");
            [[NSNotificationCenter defaultCenter]
             postNotificationName:@"dismissedDialogNotification"
             object:self];
        }
    }

}

-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    UITextField *textField = [alertView textFieldAtIndex:0];
    if (textField && [textField.text length] == 0)
    {
        return NO;
    }
    return YES;
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这是代码

- (void)textViewDidEndEditing:(UITextView *)textView {
    [textView resignFirstResponder];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {    
        [textView resignFirstResponder];
        // Return FALSE so that the final '\n' character doesn't get added
        return NO;
    }
    // For any other character return TRUE so that the text gets added to the view
    return YES;
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView {
     [textView resignFirstResponder];
     return true;
}

并在开头的警报方法中添加这行代码

[self.view endEditing:YES];