警报后打开一个新的视图控制器

Opening a new view controller after alert

在此处输入代码我无法在出现警报时返回到上一个视图控制器。

我想要做的是让用户输入数据,然后出现一个提示说它成功了,然后 return 到前一个视图控制器。

我目前没有这样做的代码,正在寻求有关我应该输入的内容的帮助。

- (IBAction)saveLabel:(id)sender
{
    NSArray *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"DATA"];
    NSMutableArray *currentDataArray;
    if (data == nil)
    {
        currentDataArray = [[NSMutableArray alloc]init];
    }
    else
    {
        currentDataArray = [[NSMutableArray alloc]initWithArray:data];
    }
    [currentDataArray addObject:self.textField.text];
    [[NSUserDefaults standardUserDefaults] setObject:currentDataArray forKey:@"DATA"];
} 

- (IBAction)enterButtonPressed:(id)sender
{
    NSLog(@"enterButtonPressed");
    UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
    [enterAlert show];
}

设置您的 UIAlertView 的委托自身

UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[enterAlert show];

使用 UIAlertviewController 的委托方法。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==0){
        // Do your Stuff Here....
        [self.navigationController popViewControllerAnimated:TRUE];
     }
}

将以下 UIAlertViewDelegate 方法添加到您的实现文件中:

- (void)alertView:(UIAlertView *)alertView
        didDismissWithButtonIndex:(NSInteger)buttonIndex {
        // If you are presenting this view controller
    [self dismissViewControllerAnimated:YES completion:nil];

    // If you are pushing this view controller
    [self.navigationController popViewControllerAnimated:YES];
    }

还要记住将 UIAlertView 委托设置为视图控制器,更改为以下代码:

UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];

//如果你使用 dismissing

[self dismissViewControllerAnimated:YES completion:^{
    UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
    [enterAlert show];
}];

//如果你使用导航,popViewController

[CATransaction begin];
[CATransaction setCompletionBlock:^{
    // handle completion here
    UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
    [enterAlert show];
}];

[self.navigationController popViewControllerAnimated:YES];

[CATransaction commit];
 UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
enterAlert.tag=100;
[enterAlert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.tag == 100) {
    if (buttonIndex == 0) {
        // Do something when ok pressed
  // If you are presenting this view controller
[self dismissViewControllerAnimated:YES completion:nil];

// If you are pushing this view controller
[self.navigationController popViewControllerAnimated:YES];
    } else {
        // Do something for other alertviewButton}

 else{// Do something with responses from other alertViews by giving tags
 }