2011-11-22 89 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