[英]UIAlertView + Modal View Controller doesn't work
我有一個UIAlertView具有按鈕“確定”和“取消”。 按下“ OK
按鈕時,我想展示一個模態視圖控制器。 到目前為止,這是我所做的:
(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
方法。 在上述方法中,當buttonIndex == 0
,我正在嘗試執行以下操作:
if (buttonIndex == 0) { ModalViewController *mdvc = [[[ModalViewController alloc] initWithNibName:nil bundle:nil] autorelease]; [self presentModalViewController:mdvc animated:YES]; }
事實證明,模態視圖本身並不存在。 我嘗試了許多其他方法,但它們只是使其變得復雜,並且使我創建了許多不必要的變量。 必須有一個更簡單的方法。
一些額外的信息:
我也有這個問題。 顯然,必須在模態出現之前就必須刪除警報,因此您正在尋找一種方法來知道警報已消失。 略讀文檔,有一種簡單的方法。
而不是在調用時顯示您的模態:
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
采用:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
這對我來說就像是一種魅力。 從文檔中:
動畫結束並隱藏視圖后,將調用此方法。
好吧...所以我自己修好了。 如我所說,這是在OpenGL ES應用程序中,因此我設置了一些全局布爾值,並在drawFrame方法中將其稱為[self presentModalViewController]
。 我知道,這絕對不是最好的方法,但是在時間緊迫的情況下,似乎沒有更好的解決方案!
這個問題當然與延遲有關,但是performSelector:withObject:afterDelay
似乎還不夠!
首先,將代碼移到名為presentModal:
的新方法中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,“確定”按鈕的索引為1。確定要對正確的按鈕索引執行操作嗎? (即,如果您按“取消”,它是否顯示模態?)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.