0

I am using the below code to display MyEditViewController but all the field outlets in MyEditViewController are nil because of this line let editController = MyEditViewController(). Here I am creating new instance. So outlets are nil now. Is there any other way to call the edit controller without creating instance of it?

@IBAction func editMethod(sender: UILongPressGestureRecognizer) {
        if sender.state == UIGestureRecognizerState.Began {
            let cell = sender.view as! MyTableViewCell
            let editController = MyEditViewController()
            editController.sample= samples[cell.tag]
            presentViewController(editController, animated: true, completion: nil)
        }
    }
Satheshkumar
  • 232
  • 2
  • 12

2 Answers2

1

You should initiate not a Class, you shoud initiate it as an ViewController from Storyboard. So in your case set an "Storyboard-ID" for your Controller.

enter image description here

And then use:

    let editController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("StoryboardID") as! MyEditViewController

So all outlets should work fine.

derdida
  • 14,784
  • 16
  • 90
  • 139
1

To instantiate a new controller with your outlets you can use a xib

let editController = MyEditViewController(nibName: "MyEditViewController", bundle: nil)

Or you can attribute a StoryboardId to your controller and use with instantiateViewControllerWithIdentifier

let editController = storyboard?.instantiateViewControllerWithIdentifier("MyEditViewControllerStoryboardId") as! MyEditViewController

Or as a suggestion for your case, you can use a different approach using storyboard segues. Read more about it here

performSegueWithIdentifier("PresentMyEditController", sender: self)
  • The 2nd solution is working fine but in`MyEditViewController` screen, I am not getting the navigation bar. – Satheshkumar Dec 18 '15 at 18:06
  • That's because you are instantiating a normal UIViewController instead of a UINavigationController. You can create a navigation with: let nav = UINavigationController(rootViewController: editController) and then presentViewController(nav, animated: true, completion: nil) – Leonardo Wistuba de França Dec 18 '15 at 18:30