我应该如何正确格式化此代码?

时间:2010-04-07 22:30:30

标签: iphone objective-c formatting

我这里有一个小问题。我在UIAlertView中使用if语句,我有两种情况,都导致UIAlertViews。但是,在一种情况下,我想只关闭UIAlertView,另一种情况是,我希望UIAlertView被解除并查看返回根视图。

此代码描述为:

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

所以,在这两种情况下,他们都遵循上述行动,但显然,这不是我想要的。关于我在这里做什么的任何想法?

2 个答案:

答案 0 :(得分:1)

您必须在clickedButtonAtIndex:方法中区分2 uialertview。

使用tag属性进行区分。

创建alerview时,为其分配标记ID:

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

类似地,

failure.tag = 2;

然后打开标签ID

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

答案 1 :(得分:0)

您可以将代码粘贴到Eclipse中,然后按ctrl+i