如何在app delegate中使用视图控制器中的函数? [英] How to use a function from the view controller in the app delegate?
问题描述
我有一个通过蓝牙连接到设备的应用。
I have an app that is connected to a device by Bluetooth.
我希望应用程序发送一个命令,指示应用程序将在app委托方法中关闭:( void)applicationWillTerminate:(UIApplication *)application {
I want the app to send a command that indicates that the app is going to close in the app delegate method : (void)applicationWillTerminate:(UIApplication *)application {
推荐答案
一个字: NSNotificationCenter
我不确定您需要将数据设置为什么,因为您无法通过 NSNotificationCenter
无缝传递数据;但无论如何你要在你的 UIApplicationDelegate
中解决这个问题,那么为什么你不能直接在视图控制器中做到这一点。
One word: NSNotificationCenter
I'm not sure what you need to set the data to because you can't pass the data seamlessly via NSNotificationCenter
; however you were going to figure that out in your UIApplicationDelegate
anyway, so why can't you do it in the view controller directly.
在您的情况下,您的应用程序委托中不需要执行任何,因为此通知允许您的视图控制器充当迷你应用代表(因为你可以获得终止状态等等)。
In your case there is no need to do anything in your application delegate, because this notification allows your view controller to act as a mini app delegate (in that you can get termination status and so on).
因此......
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TXdata:) name:UIApplicationWillTerminateNotification object:nil];
}
- (void)TXdata:(NSString *) data {
NSString *newData = data;
if (newData == nil) {
newData = ... // Figure out what your data should be here.
}
//do whatever with your data here.
}
我引用:
在应用程序即将终止时发布。
UIApplicationWillTerminateNotification
Posted when the app is about to terminate.
此通知是与委托applicationWillTerminate:方法相关联。此通知不包含userInfo字典。
This notification is associated with the delegate applicationWillTerminate: method. This notification does not contain a userInfo dictionary.
这篇关于如何在app delegate中使用视图控制器中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!