Tabbar环境下的通知提醒

发布于 2024-12-17 09:56:32 字数 2466 浏览 0 评论 0原文

我有一个关于在应用程序内显示本地通知提醒的问题。我认为问题出在视图控制器上。

这是我到目前为止的代码:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-12-24 09:56:32

由于您的警告试图告诉您 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) 您需要等到这些 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.

if (notification) {
    NSString *stringReminder = [notification.userInfo objectForKey:@"TextforReminder"];
    [viewController showReminder:stringReminder];
}
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];

UIViewController has no method named showReminder:, so I assume it's in your subclass of one of your UIViewController subclasses.

You need to do two things,

1) Replace 'viewController' with either 'viewController1' or 'viewController2' whichever has the method showReminder:

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 before return YES.

Edited as comment says SettingViewController will have the showReminder: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文