views:

109

answers:

2

Hi, I've a small issue here. I am using an if statement with UIAlertView and I have two situations, both result in UIAlertViews. However, in one situation, I want to dismiss just the UIAlertView, the other, I want the UIAlertView to be dismissed and view to return to root view.

This code describes is:

if([serverOutput isEqualToString:@"login.true"]){   

[Alert dismissWithClickedButtonIndex:0 animated:YES];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Success" message:@"The transaction was a success!"
                                                          delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[success show];
[success release];  

} else {

    UIAlertView *failure = [[UIAlertView alloc] initWithTitle:@"Failure" message:@"The transaction failed. Contact sales operator!"
                                                     delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [failure show];
    [failure release];
}
}

-(void)alertView: (UIAlertView *)success clickedButtonAtIndex: (NSInteger)buttonIndex{

switch(buttonIndex) {
    case 0: {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
} 
}

So, in both cases, they follow the above action, but obviously, that's not what I want. Any ideas on what I do here?

A: 

You could paste your code in Eclipse, and press ctrl+i.

Frederik Wordenskjold
Xcode has a re-indent command also (I think it might be ctrl+i too, but it's been a long time since I've used it..)
dbr
I tried to re-indent with Xcode first, but nothing really happened. I guess Apple loves your formatting :)Eclipse made it look a bit nicer though.
Frederik Wordenskjold
+1  A: 

You will have to differentiate between the 2 uialertview in your clickedButtonAtIndex: method.

Use the tag property to differentiate.

When you create the alerview assign a tag id to them:

UIAlertView *success = [[UIAlertView alloc] initWithTitle:@"Success" message:@"The transaction was a success!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
success.tag = 1;
[success show];

Similarly,

failure.tag = 2;

Then you switch on the tag ids

switch(alertView.tag){
   case 1: //dismiss alertview
   case 2: //dismiss alertview and return to root view
}
Mihir Mathuria