Zusee Weekin
Zusee Weekin

Reputation: 1358

error during pass variables one viewController to another controller programatically

I want to pass textfields values one viewcontroller to another. I tried following codes with no success:

Frist I tried:

var testval = "est val here lalla"
let viewController = BookingConfirmationController(nibName: "BConController", bundle: nil)
                viewController.testval = "test test"
                self.presentViewController(viewController, animated: true, completion: nil)

when I run this code I could pass variable as expect bit viewController not displayed. Instead I got black screen. I found this nibName is old usage and not use any more. I created viewContorllers through the storyborad.

Then I tried below code:

var storyboard: UIStoryboard =   UIStoryboard (name: "Main", bundle: nil)


          var testval = " test"

                var vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("BConControllerID") as UIViewController

                vc.testval = testval

                self.presentViewController(vc ,animated: true, completion: nil )

I'm having an error here as

UIViewController does not have a member named 'festival'

Without variable I can open next viewController successfully.

I'm still learning SWIFT and can anyone help me to open next view controller and pass variables

Upvotes: 0

Views: 32

Answers (1)

Ben Ong
Ben Ong

Reputation: 931

In order to have custom parameters you should have a subclass of UIViewController. To access it from another class, you have to cast that instance of UIViewController to the subclass you created. In this line

var vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("BConControllerID") as UIViewController

You should have it as something like

var vc: myViewController = storyboard.instantiateViewControllerWithIdentifier("BConControllerID") as myViewController

where myViewController is the custom subclass you have in order to access it's properties.

Also as a side note, it all you are creating is a UIViewController, you do not need to explicitly cast it as instantiateViewControllerWithIdentifier will return a UIViewController

Upvotes: 1

Related Questions