4

在我的应用程序中,我正在使用警报功能。它工作正常。当我单击右键时,它会启动我的应用程序。但我想启动不是 rootViewController 的 View Controller。我尝试在 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

4 回答 4

6

最后这是我的做法。

在 didFinishLaunchingWithOptions 中:

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

应用程序委托中的其他地方:

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

然后,在情节提要中,从分配给“rootController”的视图创建一个转场到所需的可选视图,并将该新转场的 ID 设为destinationSegue。需要进行一些调试以确保将 rootController 变量分配给正确的视图。

于 2012-09-04T10:36:39.980 回答
3

您不需要创建 segue。您只需要在情节提要中为 ViewController 分配一个 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];

通常 [[self window] makeKeyAndVisible]; 不驻留在 didFinishLaunchingWithOptions 函数中,但需要显式调用它以使 rootviewcontroller 可见。

于 2014-04-23T22:21:37.797 回答
1
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIViewcontroller *rootViewController = [window rootViewController];
[rootViewController presentModalViewController:alarmViewController animated:YES];
于 2012-08-14T15:11:53.783 回答
0

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

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

这将让您知道您的应用程序是否由于本地通知(用于警报的通知)而启动。

您还可以在以下位置执行类似操作:

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

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

于 2012-08-14T15:20:20.407 回答