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

时间:2015-05-22 04:07:53

标签: ios iphone xcode uialertview

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

我要做的是让用户输入数据,然后出现一条警告说它成功,然后返回上一个视图控制器。

我目前没有代码这样做,我正在寻求我应该提供的帮助。

- (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];
}

4 个答案:

答案 0 :(得分:1)

//如果你使用解雇

[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];

答案 1 :(得分:0)

设置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];
     }
}

答案 2 :(得分:0)

将以下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];

答案 3 :(得分:0)

 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
 }