我有一个switch语句,利用nsuserdefaults bool函数来确定打开和关闭。我的问题是当switch key bool为yes时,如何在视图控制器中调用appdelegate.m方法。基本上在视图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];
}
答案 0 :(得分:2)
[((AppDelegate*) [[UIApplication sharedApplication] delegate]) someMethod:nil];
并且不要忘记#import "AppDelegate.h
答案 1 :(得分:0)
Bool isOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"SwitchKey"];
if (isOn) {
NSLog(@"ok");
}
if (!isOn) {
[[[UIApplication sharedApplication] delegate] unregisterForRemoteNotifications];
}