如何在alertview

时间:2016-06-29 11:11:27

标签: swift

当用户按下alertView中的Go home时,我试图回到viewcontroller,它显示错误(libc ++ abi.dylib:以NSException类型的未捕获异常终止)

 @IBAction func roundButton(sender: AnyObject) {

    //add alert
    alertController = UIAlertController(title: "Return Home", message: "Are you sure???", preferredStyle: .Alert)

    let alertAction1 = UIAlertAction(title: "Cancel", style: .Default) { (action) in

    }
    let alertAction2 = UIAlertAction(title: "Go Home", style: .Destructive) { (action) in

        let nv = self.storyboard!.instantiateViewControllerWithIdentifier("storyboardidentifier") as! ViewController
        self.presentViewController(nv, animated:true, completion:nil)
    }


    alertController?.addAction(alertAction2)
    alertController?.addAction(alertAction1)

    self.presentViewController(alertController!, animated: true, completion: nil)
    //end alert

}

2 个答案:

答案 0 :(得分:0)

由于您没有提供太多信息,因此有很多可能性。我将尝试覆盖其中的大多数。

假设您在BViewController中展示提醒。你想回到AViewController

如果AViewController中嵌入了BViewControllerUINavgationController,则必须在BViewControllerAViewController之间连接展开segue。

首先,在AViewController中添加此方法:

@IBAction func unwind(segue: UIStoryBoardSegue) {

}

在故事板中,控制 - 从BViewController拖动到“AviewController”的“退出”并选择“展开:”也就是您刚刚声明的方法。这是你应该看到的

enter image description here

现在给你刚刚添加标识符的segue,说“unwindToHome”。

UIAlertAction回调闭包中,启动segue:

self.performSegueWithIdentifier("unwindToHome", sender: self)

就是这样!

如果您以模态方式呈现BViewController,那么事情会更简单,只需致电dismissViewControllerAnimated

self.dismissViewControllerAnimated(true, completion: nil)

答案 1 :(得分:-1)

在两个视图控制器之间的故事板上创建一个segue并为其指定一个标识符,然后,在警报操作的完成处理程序中,您可以触发segue。有点像:

let alertAction2 = UIAlertAction(title: "Go Home", style: .Destructive) { (action) in
    self.performSegueWithIdentifier("MyStoryboardSegue", sender: self)
}
祝你好运!

相关问题