Tabbar环境下的通知提醒
我有一个关于在应用程序内显示本地通知提醒的问题。我认为问题出在视图控制器上。
这是我到目前为止的代码:
- (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”。我知道这是因为没有视图控制器,但是我不明白我必须如何实现,提醒显示在过程中。
非常感谢您的帮助,我在这个问题上没有进一步解决。
干杯
I've got a question regarding displaying local notification reminders inside the application. I think the problem lies with the view controller.
here's the code I have so far:
- (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;
}
or:
- (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];
}
}
However I am getting errors regarding the view Controller. Use of undeclared identifier 'viewController'. I understand that this is becuase there is no view controller, however I don't unterstand how I have to achieve, that the Reminder is shown in the process.
Thanks a lot for your help, I am not getting further in this problem.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的警告试图告诉您
application:didFinishLaunchingWithOptions:
中的问题所在。UIViewController
没有名为showReminder:
的方法,所以我假设它位于您的UIViewController
子类之一的子类中。您需要做两件事,
1)将“
viewController
”替换为“viewController1
”或“viewController2
”(以具有方法为准) showReminder:
2) 您需要等到这些 viewController 真正出现在屏幕上,然后才能在它们之上呈现更多视图。因此,将上面的块移至
[self.window makeKeyAndVisible]
之后、return YES
之前。编辑评论说
SettingViewController
将具有showReminder:
方法;至于应用程序运行时接收
LocalNotifications
的问题。如果您的应用程序很简单,那么也许只需将 'viewController
' 替换为:(SettingsViewController *)[self.tabBarController.viewControllers objectAtIndex:1]
因为在您的代码中您将其添加为
viewControllers
属性的第二个元素。As your warning is trying to tell you the problem in
application:didFinishLaunchingWithOptions:
is in.UIViewController
has no method namedshowReminder:
, so I assume it's in your subclass of one of yourUIViewController
subclasses.You need to do two things,
1) Replace '
viewController
' with either 'viewController1
' or 'viewController2
' whichever has the methodshowReminder:
2) You need to wait until these viewControllers are actually on screen before presenting more views on top of them. So move the block above to after the
[self.window makeKeyAndVisible]
but beforereturn YES
.Edited as comment says
SettingViewController
will have theshowReminder:
method;As far as the problem receiving
LocalNotifications
while the app is running. If your app is simple then maybe just replace 'viewController
' with:(SettingsViewController *)[self.tabBarController.viewControllers objectAtIndex:1]
Since in your code you add it as the second element of the
viewControllers
property.