I want to call a ViewController
's member function in AppDelegate
's applicationWillResignActive
function. Not sure what is the best way to do so. I tried doing so this way:
我想在AppDelegate的applicationWillResignActive函数中调用ViewController的成员函数。不确定最好的方法是什么。我试过这样做:
let landmark = LandmarkViewController()
landmark.test()
but it somehow doesn't seem right. I'm essentially creating new instance of the controller instead of using the already existent one.
但它似乎不太合适。我实际上是创建控制器的新实例,而不是使用已经存在的实例。
4 个解决方案
#1
6
I think the best way to achieve what you want is to set an observer for this notification UIApplicationWillResignActiveNotification
into your view controller itself.
我认为实现您想要的最佳方法是将此通知UIApplicationWillResignActiveNotification的观察者设置到您的视图控制器本身。
override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(appDidResign), name: Notification.Name.UIApplicationWillResignActive, object: nil)
}
func appDidResign() {
// do your stuff
}
deinit {
NotificationCenter.default.removeObserver(self) //always remember to remove any observers (you can do this in deinit in this case)
}
#2
0
Yes, your above code will create a new instance. If you want to use an existing instance, then you'd need to get a reference to the existing instance somehow.
是的,您的上述代码将创建一个新实例。如果要使用现有实例,则需要以某种方式获取对现有实例的引用。
How you get the reference would depend on how you have your view controllers set. For example, if your LandmarkViewController
is the root view controller, you can get it via your UIWindow
, perhaps. Or, when your LandmarkViewController
instance is created, you can pass a reference to the view controller to the app delegate. It all depends on how your app is set up.
如何获得参考将取决于您如何设置视图控制器。例如,如果您的LandmarkViewController是根视图控制器,您可以通过UIWindow获取它。或者,在创建LandmarkViewController实例时,可以将对视图控制器的引用传递给应用程序委托。这一切都取决于您的应用程序的设置方式。
#3
0
You can get the rootViewController
(which your LandmarkViewController
is how I understood) from the UIWindow
:
您可以从UIWindow获取rootViewController(您的LandmarkViewController是我理解的):
if let rootViewController = self.window?.rootViewController as LandmarkViewController {
rootViewController.test()
}
#4
0
I think best way is add observer for notification in this class. Some think like this:
我认为最好的方法是在此课程中添加观察者通知。有人认为是这样的:
class LandmarkViewController: ...
{
...
func viewDidLoad()
{
...
NotificationCenter.default.addObserver(forName: applicationWillResignActive, object: nil, queue: OperationQueue.main) { (notification) in
...
}
}
}
#1
6
I think the best way to achieve what you want is to set an observer for this notification UIApplicationWillResignActiveNotification
into your view controller itself.
我认为实现您想要的最佳方法是将此通知UIApplicationWillResignActiveNotification的观察者设置到您的视图控制器本身。
override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(appDidResign), name: Notification.Name.UIApplicationWillResignActive, object: nil)
}
func appDidResign() {
// do your stuff
}
deinit {
NotificationCenter.default.removeObserver(self) //always remember to remove any observers (you can do this in deinit in this case)
}
#2
0
Yes, your above code will create a new instance. If you want to use an existing instance, then you'd need to get a reference to the existing instance somehow.
是的,您的上述代码将创建一个新实例。如果要使用现有实例,则需要以某种方式获取对现有实例的引用。
How you get the reference would depend on how you have your view controllers set. For example, if your LandmarkViewController
is the root view controller, you can get it via your UIWindow
, perhaps. Or, when your LandmarkViewController
instance is created, you can pass a reference to the view controller to the app delegate. It all depends on how your app is set up.
如何获得参考将取决于您如何设置视图控制器。例如,如果您的LandmarkViewController是根视图控制器,您可以通过UIWindow获取它。或者,在创建LandmarkViewController实例时,可以将对视图控制器的引用传递给应用程序委托。这一切都取决于您的应用程序的设置方式。
#3
0
You can get the rootViewController
(which your LandmarkViewController
is how I understood) from the UIWindow
:
您可以从UIWindow获取rootViewController(您的LandmarkViewController是我理解的):
if let rootViewController = self.window?.rootViewController as LandmarkViewController {
rootViewController.test()
}
#4
0
I think best way is add observer for notification in this class. Some think like this:
我认为最好的方法是在此课程中添加观察者通知。有人认为是这样的:
class LandmarkViewController: ...
{
...
func viewDidLoad()
{
...
NotificationCenter.default.addObserver(forName: applicationWillResignActive, object: nil, queue: OperationQueue.main) { (notification) in
...
}
}
}