我有一个关于在应用程序中显示本地通知提醒的问题。我认为问题在于视图控制器。
这是我到目前为止的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1, *viewController2;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[Number1ViewController alloc] initWithNibName:@"Number1ViewController_iPhone" bundle:nil];
viewController2 = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController_iPhone" bundle:nil];
} else {
viewController1 = [[Number1ViewController alloc] initWithNibName:@"Number1ViewController_iPad" bundle:nil];
viewController2 = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController_iPad" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
NSString *stringReminder = [notification.userInfo
objectForKey:@"TextforReminder"];
[viewController showReminder:stringReminder];
}
}
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
或:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive) {
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSString *stringReminder = [notification.userInfo
objectForKey:@"TextforReminder"];
[viewController showReminder:stringReminder];
}
}
但是我收到有关视图控制器的错误。使用未声明的标识符'viewController'。据我所知,这是因为没有视图控制器,但是我不知道如何实现,提醒过程中显示了提醒。
非常感谢你的帮助,我没有进一步解决这个问题。
干杯
答案 0 :(得分:0)
正如您的警告试图告诉您application:didFinishLaunchingWithOptions:
中的问题所在。
if (notification) {
NSString *stringReminder = [notification.userInfo objectForKey:@"TextforReminder"];
[viewController showReminder:stringReminder];
}
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UIViewController
没有名为showReminder:
的方法,因此我假设它位于您的UIViewController
子类之一的子类中。
你需要做两件事,
1)将“viewController
”替换为“viewController1
”或“viewController2
”,方法为showReminder:
2)你需要等到这些viewControllers实际上在屏幕上,然后才能在它们之上呈现更多视图。因此,请在[self.window makeKeyAndVisible]
之后return YES
之前移动上方的块。
评论时评论说SettingViewController
将采用showReminder:
方法;
在应用运行时收到LocalNotifications
的问题。如果您的应用很简单,那么可以将“viewController
”替换为:
(SettingsViewController *)[self.tabBarController.viewControllers objectAtIndex:1]
因为在您的代码中,您将其添加为viewControllers
属性的第二个元素。