从AppDelegate调用重载部分 [英] Calling reload Sections from AppDelegate
问题描述
我正在构建一个用于跟踪BLE设备的应用程序,并且有一个委托(在appDelegate中),该委托通过蓝牙更新了发现的设备的列表.
I am building an app to track BLE devices, and I have a delegate (in appDelegate) that updates the list of discovered devices over the bluetooth.
我需要在表视图中显示此设备,因此,当我发现一个设备时,我会在viewcontroller中调用一个刷新tableView的函数.
I need to display this devices in a tableview, so when I discover a device I call a function in viewcontroller that refreshes the tableView.
如果我从ViewController调用此函数,它将起作用,但是当我从AppDelegate调用该函数时,它不会执行任何操作,甚至不会出错.
If I call this function from ViewController it works, but when I call it from AppDelegate it doesn't do anything, not even an error.
AppDelegate:
AppDelegate:
//MARK: Discovered Device
func DeviceDiscovered(_ macAddress: String!, withName name: String!, ofType type: Int, andSignalStrength strength: Int) {
discoveredList["\(entry["time"]!)"] = entry
ViewController().refresh()
}
}
ViewController:
ViewController:
func refresh() {
self.tableView?.reloadSections(NSIndexSet(index: _sectionIndices.discoveredDevices.hashValue) as IndexSet, with: .none)
}
我在ViewController中添加了一个按钮,该按钮仅调用refresh()即可工作.或至少它会重新加载该部分,但createdList dict也不会在ViewController中更新,而是一次出现一个问题!
I added a button in ViewController that simply calls refresh() and it works. Or at least it reloads the section, but the discoveredList dict doesn't update in ViewController either, but one problem at a time!
感谢您的帮助!
推荐答案
问题是ViewController()
不是您的视图控制器.这是一个新的单独实例.您需要引用到您的现有视图控制器.
The problem is that ViewController()
is not your view controller. It is a new separate instance. You need to refer to your existing view controller.
这篇关于从AppDelegate调用重载部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!