繁体   English   中英

如何在应用程序委托中使用视图控制器中的功能?

[英]How to use a function from the view controller in the app delegate?

我有一个通过蓝牙连接到设备的应用程序。

我希望应用程序发送一条命令,指示该应用程序将在应用程序委托方法中关闭:(void)applicationWillTerminate:(UIApplication *)application {

一句话: NSNotificationCenter

我不确定您需要将数据设置为什么,因为您无法通过NSNotificationCenter无缝地传递数据; 但是您还是要在UIApplicationDelegate弄清楚这一点,所以为什么不能直接在视图控制器中做到这一点。

在您的情况下,您不需要在应用程序委托中做任何事情 ,因为此通知使您的视图控制器可以充当迷你应用程序委托 (因为您可以获得终止状态,依此类推)。

因此...

- (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

应用即将终止时发布。

该通知与委托applicationWillTerminate:方法相关联。 此通知不包含userInfo字典。

您应该创建一个与视图控制器和应用程序委托分开的类来处理BLE通信。 这样,视图控制器和应用程序委托都可以访问并为您的应用程序提供更好的“关注点分离”。 这个新课程可能适合单身人士。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM