1

Given the code below, when I click the "Accept" button, it doesn't navigate to SecondViewController, it only shows a black screen. Any help appreciated.

   let alertController = UIAlertController(title: tit!, message: "Book Now!", preferredStyle: .Alert)
   let declineAction = UIAlertAction(title: "Decline", style: .Cancel, handler: nil)
   alertController.addAction(declineAction)

   let acceptAction = UIAlertAction(title: "Accept", style: .Default) { (_) -> Void in
        let nv=SecondViewController()
        self.presentViewController(nv, animated:true, completion:nil)
    }
   presentViewController(alertController, animated: true, completion: nil)
   alertController.addAction(acceptAction)
Drenmi
  • 8,492
  • 4
  • 42
  • 51
Honestraj19
  • 321
  • 1
  • 3
  • 8

2 Answers2

2

To open any UIViewController just must instantiate it. And what you are doing is just creating the object of the class.

To do so:

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

This is open your UIViewController as wants!

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
0

Create a segue link from your current view controller to the SecondViewController and give it an identifier in the storyboard and you can use the following code

    let alert = UIAlertController(title: "Options", message: "Book Now!", preferredStyle: .Alert)

    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    alert.addAction(UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in
        self.performSegueWithIdentifier("FirstToSecond", sender: self)

    }))
    self.presentViewController(alert, animated: true, completion: nil)

Note: your first view controller will be the anchor.

If you have embedded your SecondViewController in a navigation controller and also want to pass the value, you can add the following

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "FirstToSecond" {
        let nav = segue.destinationViewController as! UINavigationController
        let svc = nav.viewControllers[0] as! SecondViewController
        //svc.delegate = self //uncomment this if you need to set a delegate as well
        svc.value = ""
    }
}

This is using xCode 7 & Swift 2. Hope this helps :)

Amorn Narula
  • 323
  • 3
  • 13