2010-04-07 101 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