如何从 iOS 中的警报通知操作启动视图控制器?
Posted
技术标签:
【中文标题】如何从 iOS 中的警报通知操作启动视图控制器?【英文标题】:How to launch view controller from alarm notification action in iOS? 【发布时间】:2012-08-14 14:57:21 【问题描述】:在我的应用程序中,我正在使用警报功能。它工作正常。当我单击右键时,它会启动我的应用程序。但我想启动不是 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];
【问题讨论】:
【参考方案1】:最后我是这样做的。
在 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 变量分配给正确的视图。
【讨论】:
【参考方案2】:您不需要创建 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 可见。
【讨论】:
【参考方案3】:UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIViewcontroller *rootViewController = [window rootViewController];
[rootViewController presentModalViewController:alarmViewController animated:YES];
【讨论】:
感谢您的回答 George.. 我知道这个问题很简单我想在哪里添加此代码.. 我尝试添加- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif [self presentModalViewController:alarmViewController animated:YES];
它说 Instance method '-presentModalViewController:animated:' not found (return type defaults to 'id')
试试 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
我编辑了我的问题,请看一下..我知道我做错了什么,谢谢
什么时候创建receipe view controller?
哦..我没有在任何地方创建配方视图控制器..我想在调用之前像这样创建receipeViewController = [[RecipeDetailsViewController alloc]init];
【参考方案4】:
当您的应用程序启动时,通过以下方法在 launchOptions 字典中查找 UIApplicationLaunchOptionsLocalNotificationKey:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
这将让您知道您的应用程序是否因为本地通知(用于警报的通知)而启动。
您也可以在以下位置执行类似操作:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
一旦您确定您确实收到了通知,只需从窗口中找到根视图控制器并以模态方式呈现您想要呈现的视图控制器。
【讨论】:
感谢您的回答。如何在didReceiveLocalNotification
方法中调用视图控制器.. 非常感谢
我用我的代码编辑了我的问题,请看一下。谢谢你的支持
只要初始化你想要展示的视图控制器,或者如果它已经被初始化就从它所在的位置获取它。
receipeViewController = [[RecipeDetailsViewController alloc]init]; window = [[UIApplication sharedApplication] keyWindow]; UIViewController *rootViewController = [window rootViewController]; [rootViewController presentModalViewController:receipeViewController animated:YES];
这是我在 didReceiveLocalNotification 上的代码,但不起作用是对的
发生了一些事情并进入黑屏 J2theC +1 寻求您的支持以上是关于如何从 iOS 中的警报通知操作启动视图控制器?的主要内容,如果未能解决你的问题,请参考以下文章
如何检查用户之前是不是在 iOS 中查看过推送通知权限警报视图?
如何直接通过点击通知警报打开隐藏在视图层次结构中的视图控制器
如何从 WatchOS 3 中的自定义通知操作中打开特定的视图控制器