1

Not the same as: Passing values ... and Swift - programmatically ... was not helpful for my situation.

When I press a button in one file (NSViewController)

@IBAction func bookPressed(sender: NSButton) { 
    var popVC = NSStoryboard(name: "Main", 
        bundle: nil)?.instantiateControllerWithIdentifier("PopoverViewController") as? NSViewController
    popVC.bookName = "hello"
}

I want this file to show the results of bookName = "hello"

class PopoverViewController: NSViewController {

    let bookName: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        println(bookName)
    }
}

What am I missing?

2
  • you also can use singleton :) Commented May 18, 2015 at 2:51
  • 1
    Don't use a singleton Commented May 18, 2015 at 4:27

1 Answer 1

1

You need to cast popVC as PopoverViewController so you can set the bookName property, as NSViewController does not have a property bookName:

var popVC = NSStoryboard(name: "Main", 
    bundle: nil)?.instantiateControllerWithIdentifier("PopoverViewController") as? PopoverViewController

Then you will need to present the view controller you just instantiated using presentViewController(_:animated:completion:)

Furthermore, in your PopoverViewController class, you should use var for bookName because it is immutable as is.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't realize that I needed to use presentViewController, too. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.