ios - 使用 UIAlertAction 关闭 View Controller

标签 ios swift uialertview dismiss

我正在尝试显示注销警报。当用户点击 Yes 时,我希望我的 View Controller 使用可以为我提供完成处理程序的方法关闭。

View Controller 位于导航 Controller 内,是堆栈中的第二个。

我想出了以下代码:

@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 不会为您做任何事情,因为 View Controller 不是模态显示的,而是通过导航 Controller 显示的。要关闭 is,您可以告诉导航 Controller 将其从堆栈中弹出,如下所示:

    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()
        })

关于ios - 使用 UIAlertAction 关闭 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35610276/

相关文章:

ios - uitableViewCell 转场不起作用。因为触发器?

iphone - 从未调用过 alertView didDismissWithButtonIndex

javascript - $sce.trustAsHtml 不起作用?

ios - UIView 背景颜色和顶部透明png

ios - 核心数据结果分组依据

iOS Swift float 操作按钮在更改屏幕时移动

ios - iOS 中的私有(private)唯一设备标识符

ios - UITabBarController自动生成的自定义tableview

arrays - 查找数组中所有元素实例的索引

iphone - 如何将 userInfo 添加到 UIAlertView?