2011-06-01 75 views
4

我有此應用程序完成啓動時彈出的警報視圖(免責聲明)。它的工作原理(我的應用程序現在慢得多),但如果用戶按no, thanks,我也想退出應用程序。我想我應該使用clickedButtonAtIndex :.
1.有人可以幫助我嗎?
2.是viewDidLoad應用程序啓動時觸發alertView的最佳方法嗎?
3.是否有任何理由爲什麼現在我的應用程序需要更多時間才能開始構建和運行它?警報視圖 - 如何使用clickedButtonAtIndex:

-(void)viewDidLoad { 
    UIAlertView *disclaimer = [[UIAlertView alloc] initWithTitle:   @"DISCLAIMER" message:@"This Application is provided without any express or implied warranty. Errors or omissions in either the software or the data are not guaranteed against. The application is not intented to replace official documentation or operational procedure. In no event shal the developer be held liable for any direct or indirect damages arising from the use of this application" delegate:self cancelButtonTitle:@"No, thanks" otherButtonTitles:@"Accept", nil]; 
    [disclaimer show]; 
    [disclaimer release]; 
    [super viewDidLoad]; 
} 

回答

12
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
if(buttonIndex == 0) 
// Do something 
else 
// Some code 
} 

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

    if(buttonIndex == 0) 
    // Do something 
    else 
    // Some code 
} 

確保你的類符合UIAlertViewDelegate協議。

而我不認爲從應用程序退出是一種很好的方法雖然。您應該只讓用戶通過按主頁按鈕關閉應用程序。這違背了用戶期望每個應用遵循的默認行爲。

+0

哪兒我必須之前或之後}插入此代碼.... ???我已經把委託,但在這種情況下,我不得不把這樣的事情:disclaimer.delegate = self; ???如果用戶按「不,謝謝」,我需要使用哪種方法退出應用程序? – mat 2011-06-01 10:07:27

+0

後}毫無疑問。記住這是一個單獨的方法。 :) – visakh7 2011-06-01 10:08:37

10

試試這個神奇:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(alertView.cancelButtonIndex == buttonIndex){ 
    // Do cancel 
    } 
    else{ 
    // Do the real thing 
    } 
}