如何从iOS中的警报通知操作启动视图控制器?

时间:2012-08-14 14:57:22

标签: iphone ios uilocalnotification alarm

在我的应用程序中,我正在使用闹钟功能。它工作正常。当我点击右键时,它会启动我的应用程序。但我想启动View Controller,它不是rootViewController。我试图在Google和SO搜索,但我无法得到任何想法或例子。

我正在寻找实现这一目标的任何例子。?

感谢您的帮助。

修改

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{     
// Add the view controller's view to the window and display.
[self.window addSubview:alarmViewController.view];
[self.window makeKeyAndVisible];

application.applicationIconBadgeNumber = 0;


// Handle launching from a notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
    NSLog(@"Recieved Notification %@",localNotif);

    window = [[UIApplication sharedApplication] keyWindow];
    UIViewController *rootViewController = [window rootViewController];
    [rootViewController presentModalViewController:receipeViewController animated:YES];         
}


// Override point for customization after application launch.
return YES;

}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notificaton when the app is running
NSLog(@"Recieved Notification %@",notif);   
//Called here as well
window = [[UIApplication sharedApplication] keyWindow];
    UIViewController *rootViewController = [window rootViewController];
    [rootViewController presentModalViewController:receipeViewController animated:YES];   
}

4 个答案:

答案 0 :(得分:6)

最后,我就是这样做的。

在didFinishLaunchingWithOptions中:

//save the root view controller
[[self window] makeKeyAndVisible];
UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController;
rootController = [[navigationController viewControllers] objectAtIndex:0];

app delegate中的其他地方:

[rootController performSegueWithIdentifier:@"destinationSegue" sender:self];

然后,在故事板中,从视图中创建一个segue,将其分配给“rootController”到所需的可选视图,并为新的segue提供id destinationSegue。需要一些调试才能确保将rootController变量分配给正确的视图。

答案 1 :(得分:3)

您无需创建segue。您只需在StoryController的storyboard中指定一个id。

    [[self window] makeKeyAndVisible];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    LensOverviewViewController *editLensViewController = [storyboard instantiateViewControllerWithIdentifier:@"lensOverView"];
    UINavigationController *yourViewController = [[UINavigationController alloc] initWithRootViewController:editLensViewController];
    [self.window.rootViewController presentViewController:yourViewController animated:NO completion:nil];

通常[[自我窗口] makeKeyAndVisible];不存在于didFinishLaunchingWithOptions函数中,但需要将其称为显式性以使rootviewcontroller可见。

答案 2 :(得分:1)

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIViewcontroller *rootViewController = [window rootViewController];
[rootViewController presentModalViewController:alarmViewController animated:YES];

答案 3 :(得分:0)

启动应用程序后,请使用以下方法在launchOptions字典中查找UIApplicationLaunchOptionsLocalNotificationKey:

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

如果您的应用程序因本地通知(您用于警报的通知)而启动,则会通知您。

你也可以做类似的事情:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

确定您确实收到了通知后,只需从窗口中找到根视图控制器,然后呈现您想要以模态方式呈现的视图控制器。