简体   繁体   English

在UIAlert之后执行Segue

[英]Performing Segue after UIAlert

I'm trying to move onto the next ViewController after a confirmation alert. 我试图在确认警报后移至下一个ViewController。

@IBAction func yesBtn(_ sender: Any) {
    let dialogMessage = UIAlertController(title: "Confirm", message: "Are you sure?", preferredStyle: .alert)

    let ok = UIAlertAction(title: "Confirm", style: .default, handler: { (action) -> Void in
        print("Ok button tapped")
        self.saveRecord(Answer: "yes")
        CATransaction.setCompletionBlock({
            self.performSegue(withIdentifier: "mainUse", sender: nil)
        })
    })

    let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
        print("Cancel button tapped")
    }

    //Add OK and Cancel button to dialog message
    dialogMessage.addAction(ok)
    dialogMessage.addAction(cancel)

    // Present dialog message to user
    self.present(dialogMessage, animated: true, completion: nil)
}

I've managed to perform the segue after adding the segue on the storyboard, however this performs the segue twice, once when the yes button is pressed, and another when it is confirmed in the alert box. 我已在情节提要上添加了segue之后设法执行了segue,但是这会执行两次segue,一次是在按下“是”按钮时,另一次是在警报框中确认时。 If I delete the segue on the storyboard, then the segue isn't performed at all. 如果我删除情节提要上的segue,则根本不会执行segue。 I also tried to create a custom segue by dragging the button to the next view controller and then selecting custom instead of show however this gives a SIGABRT error. 我还尝试通过将按钮拖动到下一个视图控制器,然后选择custom而不是show来创建自定义序列,但这会产生SIGABRT错误。 Obviously, it should only segue once after pressing confirm in the alert box. 显然,在警报框中按确认后,它只能进行一次检测。

I've found similar problems online but most seem to miss the part of the storyboard, am I supposed to put a link between the two views or should it all be done programmatically? 我在网上发现了类似的问题,但大多数似乎都错过了情节提要的一部分,我应该在两个视图之间建立链接还是应该以编程方式全部完成? If done programmatically exclusively, how am I supposed to identify the next view controller? 如果仅以编程方式完成,我应该如何识别下一个视图控制器?

You can achieve this by programmatically. 您可以通过编程来实现。 No need to put a link between the view controller in this case. 在这种情况下,无需在视图控制器之间放置链接。

let ok = UIAlertAction(title: "Confirm", style: .default, handler: { (action) -> Void in

    let vc = self.storyboard.instan... //get the destination view controller
    self.navigationController.push(vc, animated: true) //or you can present it using self.present(...) method
})

If done programmatically exclusively, how am I supposed to identify the next view controller? 如果仅以编程方式完成,我应该如何识别下一个视图控制器?

When you are moving from one screen to another screen there always will be destination view controller where you want to move. 当您从一个屏幕移动到另一个屏幕时,总是会有您要移动到的目标视图控制器。 So you need to get that view controller from storyboard using self.storyboard.instantia(...) method 因此,您需要使用self.storyboard.instantia(...)方法从情节self.storyboard.instantia(...)获取该视图控制器

How to get viewcontroller from storyboard? 如何从情节提要中获取ViewController?

You can do the following way 您可以按照以下方式进行

let destVC = UIStoryboard(name: "storyboard_name", bundle: nil).instantiateViewController(withIdentifier: "viewcontollerName_as_set_in_storyboard") as! DestinationViewController

How to set storyboard id to view controller? 如何设置情节提要ID以查看控制器?

在此处输入图片说明

Why do you need CATransaction ? 为什么需要CATransaction You are not doing animations yourself. 您不是自己制作动画。 You do not need CATransaction , and should not setCompletionBlock . 您不需要CATransaction ,也不应setCompletionBlock

Both alert, and segue have their own animations. 警报和segue都有自己的动画。 I suspect your completion block is being triggered on completion of multiple animations triggered by them. 我怀疑您的完成块是在由它们触发的多个动画完成时触发的。

If you just want to make sure you do things on main queue, you can use DispatchQueue.main.async instead. 如果仅要确保在主队列上执行操作,则可以改用DispatchQueue.main.async

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM