Instance variable reset to zero in ViewDidLoad ()

I have an object that inherits from UIViewController

. In this object, I have an instance variable that contains the user profile (type:) UserProfile

. I set this variable before ViewDidLoad()

, but as soon as I enter it the variable is reset to nil

...

Here is my code:

    class MapViewController: UIViewController {

        var userProfile: UserProfile!

        required init(coder: NSCoder) {
            super.init(coder: coder)
        }

        override init() {
            super.init()
        }

        override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
            super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        }

        func setProfile(userProfile: UserProfile) {        
            self.userProfile = userProfile
            //self.userProfile is OK
        }

        override func viewDidLoad() {
            super.viewDidLoad()
            self.userProfile is equal to nil....
       }
}

      

Here is the controller declaration:

self.mapViewController = MapViewController()
self.mapViewController?.setProfile(self.userProfile)
//The controller is pushed through the storyboard

      

+3


source to share


1 answer


here you create a new instance of MapViewController:

self.mapViewController = MapViewController()
self.mapViewController?.setProfile(self.userProfile)
//The controller is pushed through the storyboard

      



You have to use your target controller from segue:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) 
{
    if (segue.identifier == "showSecondViewController")
    {
        var second = segue.destinationViewController as SecondViewController
        second.setProfile(...)
    }
}

      

+2


source







All Articles