简体   繁体   中英

Performing Segue after UIAlert

I'm trying to move onto the next ViewController after a confirmation alert.

@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. If I delete the segue on the storyboard, then the segue isn't performed at all. 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. 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

How to get viewcontroller from storyboard?

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?

在此处输入图片说明

Why do you need CATransaction ? You are not doing animations yourself. You do not need CATransaction , and should not setCompletionBlock .

Both alert, and segue have their own animations. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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