【发布时间】:2017-03-15 06:27:23
【问题描述】:
我最近一直在尝试将我的应用程序迁移到 MVC 格式,我想知道如何访问 View Controller 的变量(尝试从 .swift 文件访问它们)而无需创建新的实例视图控制器。例如:
HomePageViewController().objectIDs.append(post.objectId!)
HomePageViewController().postTitles.append(post["Title"] as! String)
HomePageViewController().imageFiles.append(post["imageFile"] as! PFFile)
HomePageViewController().descriptions.append(post["description"] as! String)
HomePageViewController().userNames.append(post["originalPosterUserName"] as! String)
HomePageViewController().numOfComments.append(String(describing: post["numberOfComments"]!))
print(HomePageViewController().userNames, "This is a test")
最后的打印语句打印'[]This is a test',数组内没有数据。在 HomePageViewController 文件中,这可以完美运行。我假设这不起作用,因为我创建了 HomePageViewController 的新实例并更改了 THOSE 变量而不是当前变量。所以,我的问题是,如何在不需要创建新实例的情况下更改当前 HomePageViewController 的变量?
this 之类的解决方案也不起作用,因为我有多个具有相同变量名的类。
如果有任何帮助,我将不胜感激。谢谢!
【问题讨论】:
-
视图控制器不应该从模型以外的任何地方访问数据。
标签: swift