【问题标题】:How to get back to viewcontroller in alertview如何在 alertview 中返回 viewcontroller
【发布时间】:2016-06-29 11:11:27
【问题描述】:

当用户在 alertView 中按下 Go home 时,我尝试返回视图控制器,它向我显示错误(libc++abi.dylib: terminating with uncaught exception of type 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

}

【问题讨论】:

  • 你能告诉我异常信息吗?通常有一条消息后跟异常,就在堆栈跟踪之前。

标签: swift


【解决方案1】:

由于您没有提供太多信息,因此有很多可能性。我会尽量涵盖其中的大部分。

假设您在BViewController 中显示警报。你想回到AViewController

如果AViewControllerBViewController 嵌入在UINavgationController 中,则必须在BViewControllerAViewController 之间连接一个展开转接。

首先,在AViewController,添加这个方法:

@IBAction func unwind(segue: UIStoryBoardSegue) {

}

在情节提要中,从BViewController 控制并拖动到`AviewController 的“Exit”并选择“unwind:”,也就是您刚刚声明的方法。这是你应该看到的

现在给你刚刚添加的 segue 一个标识符,比如“unwindToHome”。

UIAlertAction回调闭包中,启动segue:

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

就是这样!

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

self.dismissViewControllerAnimated(true, completion: nil)

【讨论】:

    【解决方案2】:

    在两个视图控制器之间的情节提要上创建一个segue 并为其提供一个标识符,然后在警报操作的完成处理程序上,您可以触发转场。大致如下:

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

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2014-07-10
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      相关资源
      最近更新 更多