[英]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.