2010-04-07 125 views
0

我在这里有一个小问题。我在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]; 
    } 
} 
} 

因此,在这两种情况下,他们按照上面的动作,但显然,这不是我想要的。关于我在这里做什么的任何想法?

+0

我不知道,但几乎任何事情都比你拥有的更好。毛。 – Pyrolistical 2010-04-07 22:34:18

+0

这是怎么回事? – bear 2010-04-07 22:36:12

+0

这里有什么问题?这是关于格式化代码还是关于alertviews? – 2010-04-07 22:51:38

回答

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 
} 
0

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

+0

Xcode还有一个重新缩进命令(我认为它可能是ctrl +我也是,但它已经很长时间,因为我已经使用它..) – dbr 2010-04-07 22:48:24

+0

我试着重新缩进Xcode第一,但没有真正发生。我猜苹果爱你的格式:) Eclipse使它看起来更好看。 – 2010-04-07 23:00:55