Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenting a a ViewController on a xib file in SWIFT

I have a xib file which I have created a pop up controller on a UIView. This pop up controller animates up when a button is pressed on a View Controller 1. I then have a button on the UIView which when pressed I want to present another View Controller (View Controller 2). code looks like:

class PopUpViewControllerSwift : UIViewController {

  @IBAction func goToVC2(sender: UIButton) {

        self.removeAnimate()

        let VC2: VC2 = self.storyboard?.instantiateViewControllerWithIdentifier("VC2") as VC2
        var modalStyle: UIModalTransitionStyle = UIModalTransitionStyle.CoverVertical
        VC2.modalTransitionStyle = modalStyle
        presentViewController(VC2, animated: true, completion: nil)

    }
}

Although thou when the button is pressed it crashes, no error or callbacks or anything. Obviously this would normally work if it was just a regular View Controller but because I am doing it inside a pop Up View which has been animated on top of another View I think that is the problem?

Can anybody help?

thanks

like image 991
Henry Brown Avatar asked Mar 08 '15 12:03

Henry Brown


People also ask

How do I add a ViewController to a storyboard?

To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.

How do I insert a navigation controller in Xib?

You can add it programmatically instead. For example: // Swift let navController = UINavigationController(rootViewController: Your View Controller) // Obj-C UINavigationController *navControler = [[UINavigationController alloc] initWithRootViewController: YourViewController];


1 Answers

I did this in a UICollectionView didSelectItemAtIndexPath method.

// Register Nib
let newViewController = NewViewController(nibName: "NewViewController", bundle: nil)

// Present View "Modally"
self.present(newViewController, animated: true, completion: nil)

I set the ID of the ViewController to the same as the file name so I would be sure to reference the right ViewController.

like image 61
Michael Avatar answered Oct 18 '22 18:10

Michael