[英]Accessing ViewController class from another class in Swift
I'm new in iOS development, 我是iOS开发的新手,
I have and "API Helper" Swift class that gets some data as a JSON array. 我有一个“ API Helper” Swift类,该类获取一些数据作为JSON数组。 And when the array is ready I want to call a method in my MasterViewController to update the tableView with the data.
当数组准备好时,我想在我的MasterViewController中调用一个方法来用数据更新tableView。
I tried to do like that: 我试图这样做:
var facilities : [Facility]? {
didSet {
MasterViewController().facilitiesLoaded()
}
}
And then reload the tableView but without seeing anything. 然后重新加载tableView但什么也看不到。
I think the problem is that I'm creating a new instance of the ViewController, but what I should have is to get access to the current instance of the class. 我认为问题在于我正在创建ViewController的新实例,但是我应该拥有的是访问类的当前实例的权限。
Any idea, or a better design? 有什么想法或更好的设计吗? Thanks..
谢谢..
If this "facilities" variable is instance of MasterViewController then do : 如果此“设施”变量是MasterViewController的实例,请执行以下操作:
var facilities : [Facility]? {
didSet {
self.facilitiesLoaded()
}
}
Thanks to @dcestari, 感谢@dcestari,
I did added callback blocks to the API call and handled it in the ViewController and that did the trick 我确实向API调用添加了回调块,并在ViewController中对其进行了处理,从而达到了目的
In API caller method: 在API调用者方法中:
func loadFacilities(completionHandler:(() -> Void!)) {
// do stuff
completionHander()
}
In the ViewController: 在ViewController中:
func getFacilities() {
api.loadFacilities({
// update tableView
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.