2011-09-15 27 views
0

我有一個UIAlertView有按鈕「確定」和「取消」。我想在按下按鈕OK時顯示模式視圖控制器。這是我迄今爲止所做的:UIAlertView +模態視圖控制器不起作用

  1. 創建UIAlertView框。實現了UIAlertViewDelegate協議。實施(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex方法。
  2. 在上述方法中,當buttonIndex == 0,我試圖做一些事情的影響:

    if (buttonIndex == 0) 
    { 
        ModalViewController *mdvc = [[[ModalViewController alloc] initWithNibName:nil bundle:nil] autorelease]; 
        [self presentModalViewController:mdvc animated:YES]; 
    } 
    

事實證明,模態視圖不存在本身。我嘗試了許多其他方法,但它們只是使它變得複雜,並使我創建了大量不必要的變量。必須有一個更簡單的方法。

一些額外的信息:

  1. 如果反正重要的,這是一個OpenGL ES應用程序。
  2. 如果我調用[self presentModalController:]作爲UIButton按下的結果,它確實按預期工作 - 我看到了模式視圖控制器。

回答

1

好吧......所以我自己修復了這個問題。正如我所說,這是在OpenGL ES應用程序中,所以我設置了一些全局布爾值,並在drawFrame方法中調用[self presentModalViewController]。這絕對不是最好的方法,但我知道,在時間緊縮的情況下,似乎沒有更好的解決方案!

這個問題肯定與延遲有關,但performSelector:withObject:afterDelay似乎不夠用!

0

首先,該代碼移動到一個名爲presentModal:

- (void)presentModal:(UIViewController *)mvc { 
    [self presentModalViewController:mvc animated:YES]; 
} 


然後,在你處理從UIAlertView中的響應方法的新方法,把這種新的方法,這樣

ModalViewController *mdvc = [[[ModalViewController alloc] 
              initWithNibName:nil 
                bundle:nil] autorelease]; 
[self performSelector:@selector(presentModal:) 
      withObject:mdvc 
      afterDelay:0.0]; 


這將推遲執行presentModal:方法,直到處理UIAlertView的方法已經返回,並且UIAlertView已經從屏幕上移除。

+0

我已經試過這個方法,它似乎沒有工作。但是,如果我通過UIButton按鈕調用presentModalController,它確實按預期工作 - 我看到了模式視圖控制器。 – Ravi

0

「取消」按鈕的索引爲0,「確定」按鈕的索引爲1.是否確定要對正確的按鈕索引執行操作? (即如果您按'取消'它是否顯示模式?)。

+1

是的。這已經過檢查。按鈕和索引被適當地映射。 – Ravi

5

我也有這個問題。看起來很清楚,警報必須在模態顯示之前消失,所以您正在尋找一種方法來了解警報已消失。瀏覽文檔,有一個簡單的方法。

相反出示您的模式時,這就是所謂的:

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

用途:

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

這個工作對我來說就像魅力。從文檔:

該方法在動畫結束並且隱藏視圖爲 之後被調用。