1

I've got 2 view controllers. The main one mainMenuViewController, and the secondary one ViewController.

The secondary view controller has an alert that is sometimes displayed, and when the user selects "Cancel", I want the mainMenuViewController to be displayed.

How can I achieve this with code?

So far I've got the following:

       let refreshAlert = UIAlertController(title: "You Win!", message: "Do you want to play again?", preferredStyle: UIAlertControllerStyle.Alert)

        refreshAlert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: { (action: UIAlertAction!) in
            self.restart()
        }))

        refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
            print("Handle Cancel Logic here")
        }))

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

When the user clicks Cancel, I've temporarily printed the line print("Handle Cancel Logic here"), but I need the mainMenuViewController to become active.

I've tried creating a function to do this, but it won't work, can you suggest why?

@IBAction func quit(sender: UIBarButtonItem) {
    let vc = mainMenuViewController()
    self.presentViewController(vc, animated: true, completion: nil)
}

Any help appreciated!

  • 1
    "but it won't work" What does that mean? – matt Jun 27 '16 at 02:42
  • Why not use the presentViewController function instead of printing that message ? – Sam Fischer Jun 27 '16 at 02:44
  • When I try the function, it displays an all black screen, not the mainMenuViewController. I would use the presentViewController but I'm testing the change of view controller from a button, as to display the alert takes a long time. Once i can change view controllers by clicking a button, I will move that code into the alert function. –  Jun 27 '16 at 02:54
  • I've managed to do it by creating a segue from the button to the mainMenuViewController, and then call self.performSegueWithIdentifier("returnToMainVC", sender: nil) when I want to change back to the mainMenuViewController. One slight problem though. The title of the previous viewController is displayed as a back button in the top left (like when you go into Bluetooth settings, the word "settings" is displayed in the top left. How can I get rid of that? –  Jun 27 '16 at 03:02

2 Answers2

2

You cannot just call the initializer on a VC class initialize a VC. It is not the correct way to do it.

I assuming you have a storyboard called Main.storyboard. If you don't have one, create one and add the MainViewController. You should give your MainViewController an identifier:

enter image description here

First, you need to get your storyboard:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("mainVC")

Now vc is the MainViewController! You can present it by presentViewController.

Alternatively, you can use segues!

Connect your SecondaryViewController and MainViewController with a segue. Give it an identifier and call

performSegueWithIdentifier("some identifier", sender: self)
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • I think you should use the storyboard ID not the restoration ID and if you do use the latter you have to check the checkbox – Daniel Jun 27 '16 at 05:14
  • @Dopapp I accidentally put the cursor there when taking the screenshot and I didn't realize it... Fixed now! – Sweeper Jun 27 '16 at 05:17
  • Thanks for the advice. I have implemented that, but it transitions in the wrong way. I don't know what the name of the transition or segue is called, but its the same transition you get when you click into one of the system settings (i.e. Bluetooth) and then you get a back button appear on the top left of the navigation bar - see my screen capture - https://imgur.com/VAbnPCI. Do you know how I can change the transition, or hide the top button shown in the image? –  Jun 27 '16 at 05:26
  • @PabloEscobar For that you need a `UINavigationController`. Just Google "implement navigation controller ios swift" – Sweeper Jun 27 '16 at 05:27
  • Thanks very much!! –  Jun 28 '16 at 05:36
0

In Swift 3 with Storyboard:

  1. Give your second view-controller a storyboardID. (assuming its called "secondVC")
  2. In the first view-controller's code, when you want to switch, write:

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let secondVC = storyBoard.instantiateViewController(withIdentifier: "secondVC")
    self.present(mainVC, animated: true, completion: nil)
    
Lavi Avigdor
  • 4,092
  • 3
  • 25
  • 28