2012-04-27 162 views
2

我在iOS 5上成功收到我的通知。我想要在用户滑动或点按通知中心内的推送通知时能够将用户发送到特定视图。从推送通知中推送视图

视图控制器(视图)我希望用户去反对刚开始我的应用程序是“groceryStoreViewController”。我已经读过,这是在didFinishLaunchingWithOptions或didReceiveRemoteNotification中完成的,但我不确定。

如果有人知道如何做到这一点,我真的很感激它,因为它确实是一场斗争。

感谢

编辑

所以,问题是,我想,当用户点击一个通知要打开特定视图控制器,但我也希望UITabBar保持。我还没有成功地做到这一点,这与我相信显示子视图有关。请让我知道你的想法,并非常感谢你。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

self.tabBarItem = [[[UITabBarItem alloc] init] autorelease]; 

exploreViewController *view1 = [[exploreViewController alloc] initWithNibName:@"exploreViewController" bundle:nil]; 
view1.title= @"Explore"; 

Upcoming *view2 = [[Upcoming alloc] initWithNibName:@"Upcoming" bundle:nil]; 
view2.title = @"Upcoming"; 

TipsViewController *view3 = [[TipsViewController alloc] initWithNibName:@"TipsView" bundle:nil]; 
view3.title = @"Tips"; 

UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:view1]; 
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:view2]; 
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:view3]; 

[view1 release]; 
[view2 release]; 
[view3 release]; 

self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nil]; 
self.tabBarItem = [[[UITabBarItem alloc] init] autorelease]; 

[nav1 release]; 
[nav2 release]; 
[nav3 release]; 


if (launchOptions != nil) 
{ 
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 
NSLog(@"Launched from push notification"); 
//Accept push notification when app is not open 
if (remoteNotif) {  

NSDictionary *alertBody = [remoteNotif objectForKey:@"loc-key"]; 

self.window.rootViewController = nav2; //this is what I want displayed when tapped but also maintain tab bar controller 
    [window addSubview:tabBarController.view]; 
    [window makeKeyAndVisible]; 

    } 
} 
else { 

    //Go here if just loading up normally without push 
    [window addSubview:tabBarController.view]; 
    [window makeKeyAndVisible]; 

} 
    return YES; 

} 
+0

问题没有得到很好的定义! – 2012-04-27 06:31:06

+3

@hp iOS编码器:并不是每个用户在这里都能很好地说英语。所以请善待,并给它一个镜头.. – filou 2012-05-14 11:51:44

回答

3

它在didFinishLaunchingWithOptions:方法完成。您可以检查是否因通知而启动应用程序,并设置适当的viewController以显示。

喜欢的东西:

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    // other stuff 

    if (launchOptions != nil) { 
     NSLog(@"Launched from push notification"); 
     NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
     // Do something with the notification dictionary 
     self.myViewController = [LaunchFromNotificationViewController alloc] init]; 
    } else { 
     self.myViewController = [OrdinaryLaunchViewController alloc] init]; 
    } 

    self.window.rootViewController = self.myViewController; 
    [self.windows makeKeyAndVisible]; 
    return YES; 
} 
+0

你能举个例子吗?意味着很多。谢谢 – 2012-04-27 07:27:31

+0

@AlexG示例添加。 – jcm 2012-04-29 14:22:23

+0

@ jcm:我试图用新的故事板功能使用你的代码。该视图的代表团不起作用..你有故事板的经验吗? – filou 2012-05-14 12:08:09