简体   繁体   English

从Swift View Controller将数据传递到Objective-C View Controller

[英]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: 我正在尝试使用以下代码将数据从Swift视图控制器(viewControllerA)传递到Objective-C视图控制器(viewControllerB):

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. 在调用prepareForSegue之后,我的属性(dictionaryB和arrayB)变为null。 How can I fix this? 我怎样才能解决这个问题?

You grab the destination VC, navController , but then do nothing with it. 您可以获取目标VC navController ,但随后对其不执行任何操作。

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). 然后,代码创建一个ViewControllerB ,为其分配字典和数组,但随后对viewControllerB不执行任何viewControllerB ,因此将在此方法的末尾简单地将其释放(根据我从发布的代码中可以看到的)。

There's also a line viewControllerB.navigationController?.topViewController which appears to do nothing. 还有一行viewControllerB.navigationController?.topViewController似乎什么也没做。

I'm guessing you hoped to do something like: 我猜您希望这样做:

viewControllerB.navigationController?.topViewController = viewControllerB

But that won't compile; 但这不会编译。 you actually need: 您实际上需要:

navController.viewControllers = [viewControllerB].

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Swift的Objective-c基本视图控制器继承 - Inheritance from an Objective-c base view controller from Swift 将数据从Objective C中的视图控制器传递到Swift中的控制器 - Passing data from view controller in Objective C to controller in Swift 将变量传递给Objective-C中嵌入在导航控制器中的View Controller - Pass variable to View Controller Embedded in Navigation Controller in Objective-C ios从第二个视图控制器向第一个视图控制器发送Objective-c数据发送 - ios Delegate Objective-c data sending from 2nd view controller to 1st view controller 从目标C视图控制器引用Swift视图控制器 - Referencing a Swift View Controller from an Objective C View Controller 在Objective-C中调用swift视图控制器文件的变量 - Calling to a variable of swift view controller file in Objective-C iOS / objective-c:将对象传递到新的视图控制器 - iOS/objective-c: Pass object to new view controller Objective-C:在prepareForSegue中将地图快照传递给视图控制器 - Objective-C: Pass a map snapshot to view controller in prepareForSegue 不在视图控制器上呈现视图控制器 - Swift / Objective-C - Present View Controller without being on a View Controller - Swift / Objective-C 无法从 Objective-C 视图控制器访问 Swift var - iOS - Cannot access Swift var from Objective-C View controller - iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM