2011-12-12 53 views
1

我想顯示警報,並且當有人點擊確定時,他們需要先發送到頁面。我怎樣才能做到這一點?顯示警報xcode並在警報後返回

我使用下面的代碼:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"BOOYAH!" 
               message:@"Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[alert show]; 
+0

你用什麼來處理你的'網頁'? –

+1

我們需要更多關於您的觀點的信息。 –

回答

2

考慮到你對警報視圖選項和委託是自。在與上面的代碼相同的.m文件中使用此方法

- (void)alertView:(UIAlertView *)alertV didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    //go back a page 

    [alertV autorelease]; 

} 

不要忘記釋放警報視圖。我在代理方法中添加了它,但您可以選擇在顯示它之後立即發佈它(儘管只有一個版本)

0

分配UIAlertViewDelegate自我,然後實現下面的方法被稱爲

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

    if (buttonIndex == buttonIndexForOK) { // Where buttonIndexForOK is the index for your ok button, in this case, that would be zero, but if you want an OK and a Cancel button this would be different. 
      // go back to the last page 
    } 

} 
0

UIAlertView遵循iOS開發中極爲常見的委託設計模式。您提供了一個委託對象,並且當對象想要告訴您某件事情時,它會向該委託對象發送一條消息。

在您的代碼中,您提供了self作爲委託。這意味着此對象需要符合the UIAlertViewDelegate protocol

您會看到有幾種方法可以實現對與警報視圖相關的各種事件作出反應。您應該使用the alertView:clickedButtonAtIndex: method,它提供了一個指示哪個按鈕被點擊的索引參數。