2011-11-22 60 views
0

我有一个关于在应用程序内部显示本地通知提醒的问题。我认为问题在于视图控制器。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'。我明白,这是因为没有视图控制器,但我并不是不知道该如何实现,而是在过程中显示提醒。

非常感谢您的帮助,我没有进一步解决这个问题。

干杯

回答

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属性的第二个元素。

+0

谢谢您的快速回复! /我已经完成了你所说的,但我仍然收到两个错误:在第一部分中,我得到:实例消息的接收器类型'UIViewController'没有声明一个选择器'showReminder:'的方法,第二部分是:没有已知的实例方法选择器'showReminder:'//我非常感谢你的帮助,我知道我是一个新手,但我试图尽可能地学习,但有时它不可能没有专业人员的帮助:)欢呼 –

+0

还是我必须在相应的视图控制器中自己创建showReminder:方法? –

+0

Number1ViewController或SettingsViewController是否有一个'showReminder:'方法?既然他们是你的课程,你只能知道。你是否也在'didFinishLaunchingWithOptions:''''didReceiveLocalNotification:''中获得了这些警告? – NJones