【问题标题】:How to call a method from appdelegate [duplicate]如何从appdelegate调用方法[重复]
【发布时间】:2013-03-25 16:37:23
【问题描述】:

我有一个 switch 语句,它利用 nsuserdefaults bool 函数来确定打开和关闭。我的问题是当 switch 键 bool 为 yes 时,如何在视图控制器中调用 appdelegate.m 方法。基本上在 view controller.m 的第一个 if 语句中调用 appdelagte.m 方法。

Appdelegate.m

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
     TUPushHelper * helper = [[TUPushHelper alloc] initWithTokenData:devToken];
     [helper registerDevice];
}

Viewcontroller.m

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"SwitchKey"]) {
    NSLog(@"ok");   
}
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"SwitchKey"]) {
    [[UIApplication sharedApplication]unregisterForRemoteNotifications];   
}

【问题讨论】:

标签: iphone ios objective-c xcode push-notification


【解决方案1】:
[((AppDelegate*) [[UIApplication sharedApplication] delegate]) someMethod:nil];

别忘了#import "AppDelegate.h

【讨论】:

    【解决方案2】:
    Bool isOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"SwitchKey"];
    
    if (isOn) {
        NSLog(@"ok");   
    }
    
    
    if (!isOn) {
        [[[UIApplication sharedApplication] delegate] unregisterForRemoteNotifications];   
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多