简体   繁体   中英

Opening a new view controller after alert

enter code hereI am having trouble with going back to the previous view controller when alert is presented.

What I am trying to do is have the user enter in data, then an alert appear saying it was successful, then return to the previous view controller.

I currently have no code doing so and am seeking assistance with what I should put in.

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

//If ur using dismissing

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

//if ur using navigation ,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];

Set Delegate self of your UIAlertView

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

Use Delegate Method of UIAlertviewController.

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

Add the following UIAlertViewDelegate method to your implementation file:

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

Also remember to set your UIAlertView delegate to your view controller, change to the following code:

UIAlertView *enterAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Entry was recorded" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
 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
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM