我的应用程序有导航控制,我有 2 个 UIViewController。 viewCon1 是初始 View Controller 。
然后我将 viewCon2 插入导航控制中。现在我想在没有静态逻辑的情况下访问 viewCon2 中 viewCon1 的变量和函数。
我使用静态函数逻辑来获取 viewCon1 的 Self,即 WK_DshBrd
let dashBoardObj = WK_DshBrd.getDasBoardObject() as! WK_DshBrd
dashBoardObj.unReadMsgsCaseArr?.append(caseNo)
但是我在运行时遇到以下错误
Could not cast value of type 'Watch_Extension.WK_DshBrd' (0x7f1e0) to 'Watch_Extension.WK_DshBrd' (0x7ed78).
我如何转换它或以其他方式实现所需的功能?
最佳答案
您可以在第一个 Controller 中使用prepareForSegue方法。在那里你可以像这样检索第二个 View Controller :
let controller = segue.destinationViewController as! SecondViewController
controller.data = "test" //PASS DATA HERE
最终您甚至可以在此处传递对第一个 View Controller 的引用,但不建议这样做。
编辑:
当您使用 watchkit 时,您可以在上下文变量中传递数据。检查一下这个教程:http://www.tech-recipes.com/rx/52810/how-do-i-pass-data-between-screens-in-my-apple-watch-apps/
关于ios - 在其他 UiViewController 中访问 UIViewController 的函数和变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33689155/