ViewController().grabData()将创建ViewController的新实例并调用此函数。然后,由于不使用视图控制器,它将被垃圾回收/从内存中删除。您需要在使用中的实际视图控制器上调用此方法。不是它的新实例。
最好的选择是监听
UIApplicationDidBecomeActiveiOS提供的通知。
NotificationCenter.default.addObserver( self, selector: #selector(grabData), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
确保您也删除了观察者,这通常是通过一种
deinit方法完成的
deinit() { NotificationCenter.default.removeObserver(self)}


