2013-03-25 48 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

相關問題