簡體   English   中英

解雇由模態視圖控制器呈現的UIAlertController

[英]dismiss UIAlertController presented by a modal view controller

我似乎遇到了類似於一個未解決的問題中的問題: UIAlertController解雇他的presentViewController

我在普通的UIViewController上呈現模態視圖控制器。 然后我在該模態視圖控制器上彈出警報。 當我按“ok”關閉警報(使用下面的代碼生成)時,模態視圖控制器也被解除。

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK"
                                             style:UIAlertActionStyleDefault 
                                           handler:^(UIAlertAction *action{ 
                                                    [self dismissViewControllerAnimated: YES completion: nil];}];

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Sign up problem."
                                                               message:@"Some fields are empty. Please check your inputs and try again."
                                                        preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];

我該如何解除警報?

我意識到我可以通過使用導航控制器類型設置而隱藏導航欄來避免這個問題,所以我得到了與模態視圖控制器相同的感覺,但這看起來很傻。 謝謝。

不要在按鈕處理程序中調用self dismissViewController 這特別聲明您希望視圖控制器被解雇。

您無需關閉警報。 它會自動解散自己。 您應該在按鈕處理程序中執行的唯一操作是執行您需要的任何操作。 如果你不需要做任何事情,什么也不做。

如果您的警報只是一條消息而您不需要執行任何操作,請執行以下操作:

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

您無需在按鈕處理程序中以任何方式手動關閉或刪除UIAlertController - 它本身就是這樣做的。

只需刪除對dismissViewControllerAnimated:completion:的調用dismissViewControllerAnimated:completion: .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM