Navigate to ViewController from UIAlertController button click

微笑、不失礼 提交于 2019-12-21 05:57:09

问题


I am quite new to Swift development and I tried referring to the Swift UIAlertController API but couldn't figure out how to navigate to another UIViewController after clicking a button on the UIAlertController.

I would appreciate any pointers, help or a solution to this problem. My code snippet is given below -

@IBAction func showAlert() {

    let alertController = UIAlertController(title: "Disclaimer", message: "Disclaimer Text Here", preferredStyle: .Alert)


    let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
    alertController.addAction(declineAction)

    let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in

        let secondVC = ViewController(nibName: "ViewController", bundle: nil)
        let navController = UINavigationController(rootViewController: secondVC)
        self.presentViewController(navController, animated: true, completion: nil)

    }
    alertController.addAction(acceptAction)

    presentViewController(alertController, animated: true, completion: nil)

}

What I am trying to do here is when a button is clicked the disclaimer AlertController is displayed. If "Decline" button is selected, cancel action is performed and if "Accept" is selected the app should navigate to a Navigation Controller which then allows me to navigate to other ViewControllers using a menu in the application.

Earlier I used the story board to link a button to the NavigationController to traverse to the ViewController I desired. Now I want to do the same programmatically for the AlertController "Accept" Button.

Thank you in advance.


回答1:


You need to implement the handler block to perform code when an action is selected :

@IBActionfunc showAlert() {

    let alertController = UIAlertController(title: "Disclaimer", message: "Disclaimer Text Here", preferredStyle: .Alert)


    let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
    alertController.addAction(declineAction)

    let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in

        let secondVC = SecondViewController(nibName: "SecondView", bundle: nil)
        let navController = UINavigationController(rootViewController: secondVC)
        self.presentViewController(navController, animated: true, completion: nil)
    }
    alertController.addAction(acceptAction)

    presentViewController(alertController, animated: true, completion: nil)

}



回答2:


Basically to link the UIAlertViewController button to a UINavigationController's UIViewController we would need to create a manual segue between the UIViewController which has the UIAlertViewController to the UINavigationController.

Please refer to this screen shot to see how to do the above -

Then select the link between the UIViewController and the UINavigationController. Go to the left sidebar and select the Attributes inspector and name the identifier.

Now write the following in your code -

@IBAction func showAlert() {

let alertController = UIAlertController(title: "Disclaimer", message: "Before using this teaching resource, you confirm that you agree:\n1. To obey the law regarding data protection and patient confidentiality.\n2. To us this app professionally and appropriately in clinical settings.\n3. This is for your personal use and you may not modify, distribute, publish, transfer any information obtained from this teaching resource without the developers' permission.\n4. In no event shall the developer be liable to you for any loss arising from your use of this resource.", preferredStyle: .Alert)

let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in    

    self.performSegueWithIdentifier("SomeSegue", sender: self) // Replace SomeSegue with your segue identifier (name)
}

alertController.addAction(declineAction)
alertController.addAction(acceptAction)

presentViewController(alertController, animated: true, completion: nil)
}



回答3:


That's the use of the handler block. You should make your desired action in this block.

Edit: Code

let acceptAction = UIAlertAction(title: "Accept", style: .Default, handler:{  action in 
    //Write your code here
})

You can use this link as a reference: http://www.appcoda.com/uialertcontroller-swift-closures-enum/



来源:https://stackoverflow.com/questions/28437104/navigate-to-viewcontroller-from-uialertcontroller-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!