簡體   English   中英

使用UIAlertAction關閉視圖控制器

[英]Dismiss a View Controller with a UIAlertAction

我正在嘗試提示退出警報。 當用戶點擊是時 ,我希望我的視圖控制器使用可以為我提供完成處理程序的方法來解除。

視圖控制器位於導航控制器內,是堆棧中的第二個。

我想出了以下代碼:

@IBAction func logOut() {
        let logOutAlert = UIAlertController.init(title: "Log out", message: "Are you sure ?", preferredStyle:.Alert)

        logOutAlert.addAction(UIAlertAction.init(title: "Yes", style: .Default) { (UIAlertAction) -> Void in
            //Present entry view ==> NOT EXECUTED
            self.dismissViewControllerAnimated(true, completion:nil)
        })

        logOutAlert.addAction(UIAlertAction.init(title: "Cancel", style: .Cancel, handler: nil))

        self.presentViewController(logOutAlert, animated: true, completion: nil)
}

self.dismissViewControllerAnimated(true, completion:nil)被讀取,但它沒有做任何事情。

我懷疑dismissViewControllerAnimated沒有為你做任何事情,因為視圖控制器沒有以模態方式呈現,而是通過導航控制器顯示。 要解雇是,您可以告訴導航控制器從堆棧中彈出它,如下所示:

    logOutAlert.addAction(UIAlertAction.init(title: "Yes", style: .Default) { (UIAlertAction) -> Void in
        self.navigationController?.popViewControllerAnimated(true)
        })

不幸的是, popViewControllerAnimated似乎沒有提供一種方法來附加你自己的完成處理程序開箱即用。 如果你需要一個,你仍然可以通過利用相關的CATransaction添加一個,它看起來像這樣:

    logOutAlert.addAction(UIAlertAction.init(title: "Yes", style: .Default) { (UIAlertAction) -> Void in
        CATransaction.begin()
        CATransaction.setCompletionBlock(/* YOUR BLOCK GOES HERE */)
        self.navigationController?.popViewControllerAnimated(true)
        CATransaction.commit()
        })

暫無
暫無

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

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