2013-03-25 50 views
0

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

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]; 
} 
+0

发现了另一个问题:http://stackoverflow.com /问题/ 8233253 /调用一个函数合的appdelegate?RQ = 1 – peko 2013-03-25 16:55:22

回答

0
Bool isOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"SwitchKey"]; 

if (isOn) { 
    NSLog(@"ok"); 
} 


if (!isOn) { 
    [[[UIApplication sharedApplication] delegate] unregisterForRemoteNotifications]; 
} 
2
[((AppDelegate*) [[UIApplication sharedApplication] delegate]) someMethod:nil]; 

,不要忘了#import "AppDelegate.h

相关问题