简体   繁体   中英

Pass Data To Objective-C View Controller From Swift View Controller

I am trying to pass data from a Swift View Controller (viewControllerA) to a Objective-C View Controller (viewControllerB) with the following code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "ViewControllerB") {

        var navController : UINavigationController! = UINavigationController()

        navController = segue.destinationViewController as! UINavigationController

        let viewControllerB : ViewControllerB! = ViewControllerB()

        viewControllerB.navigationController?.topViewController

        // Not sure how to alloc init code in an Objective-C Class in Swift file

        viewControllerB.dictionaryB = [NSObject : AnyObject]()
        viewControllerB.arrayB = [AnyObject]()

        viewControllerB.dictionaryB = self.dictionaryA
        viewControllerB.arrayB = self.arrayB

    }
}

My properties (dictionaryB & arrayB) are coming up null after prepareForSegue is called. How can I fix this?

You grab the destination VC, navController , but then do nothing with it.

The code then creates a ViewControllerB , assigns dicts & arrays to it, but then does nothing with viewControllerB , so it will simply be deallocated at the end of this method (from what I can tell from the posted code).

There's also a line viewControllerB.navigationController?.topViewController which appears to do nothing.

I'm guessing you hoped to do something like:

viewControllerB.navigationController?.topViewController = viewControllerB

But that won't compile; you actually need:

navController.viewControllers = [viewControllerB].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM