简体   繁体   English

如何根据远程通知在ios应用程序中更改初始视图控制器?

[英]How do you change the initial view controller in an ios application based on a remote notification?

I have an application that is implementing remote notifications. 我有一个正在实现远程通知的应用程序。 My desire is to change the initial view controller based on one of three conditions: the user started the app on their own (no notifications); 我的愿望是根据以下三个条件之一来更改初始视图控制器:用户自行启动应用程序(无通知); or, the user received an suspicious activity alert; 或者,用户收到了可疑的活动警报; or, the user received a crime alert. 或者,用户收到了犯罪警报。 I have implemented the application didReceiveRemoteNotification: method in the appDelegate. 我已经在appDelegate中实现了应用程序didReceiveRemoteNotification:方法。 Based on the notification and the user response to the notification I have implemented the following code: 基于通知和用户对通知的响应,我实现了以下代码:

UITabBarController *tbContr = (UITabBarController*) self.window.rootViewController;
UINavigationController *navContr = [tbContr.viewControllers][2];
ViewCrimesController *viewCrimes = [navContr.storyboard instantiateViewControllerWithIdentifier:@"ViewCrimes"];
[navContr presentViewController:viewCrimes animated:YES completion:nil];

[self.window.makeKeyAndVisible];

The problem I am having is that the navigation controls -- ie, the back button and the navigation bar title; 我遇到的问题是导航控件-即返回按钮和导航栏标题; are not on the ViewCrimesController when it is presented. 呈现时不在ViewCrimesController上。 I have tried to load the ViewCrimesController many different ways. 我试图以多种不同方式加载ViewCrimesController。 Each way I get either and error saying there is no segue (this view is a model view to map view) or I am attempting to load the over an active view or, again, I don't get the navigation controls. 无论哪种方式,我都会收到错误提示说没有segue(此视图是映射视图的模型视图),或者我试图在活动视图上加载该视图,或者再次没有导航控件。

Do I need to specifically program the navigation controls or am I missing something in the way I am attempting to load the view? 我是否需要对导航控件进行专门编程,还是在尝试加载视图时缺少某些东西?

I have seen references to dynamically changing the initial view in other posts. 我在其他文章中看到了有关动态更改初始视图的参考。 But I have not seen anything that indicates specific programming is required to add the controls. 但是我还没有看到任何指示需要添加控件的特定编程的东西。 Any help you can provide is greatly appreciated! 您能提供的任何帮助将不胜感激!

Susan 苏珊

Have you looked at the post, Programmatically set the initial view controller using Storyboards 您是否看过帖子, 使用情节提要以编程方式设置初始视图控制器

The answer from that post may solve your issue, 该帖子的答案可能会解决您的问题,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];

   self.window.rootViewController = viewController;
   [self.window makeKeyAndVisible];

   return YES;
}

You can also download this sample, and implement it as per your requirement. 您还可以下载此样本,然后根据需要实施它。

simply change the key window's rootViewController, sample code placed here, hope it helps: 只需更改键窗口的rootViewController,示例代码放在此处,希望对您有所帮助:

UIStoryboard *storyboard = self.window.rootViewController.storyboard;
    UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"rootNavigationController"];
    self.window.rootViewController = rootViewController;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在iOS中基于远程通知导航复杂的视图层次结构? - How do I navigate complex view hierarchy based on remote notification in iOS? iOS:如何从另一个视图控制器类更改视图大小/来源? - iOS:How do you change a view size/origin from another view controller class? 如何根据条件设置初始视图控制器? - How to set initial view controller based on conditions? iOS - 如何控制模态视图控制器的大小? - iOS — how do you control the size of a modal view controller? 如何添加从启动屏幕到初始View Controller iOS的过渡? - How to add transition from launch screen to initial view controller ios? iOS Storyboard更改初始视图 - iOS Storyboard change initial view 您如何在iOS应用的通知中心中更改推送通知的图标? - How do you change icons for push notifications in the Notification Center for iOS apps? 如何在 iOS 远程通知上禁用默认通知警报视图? - How to disable default notification alert view on iOS remote notifications? 如何从iOS中的警报通知操作启动视图控制器? - How to launch view controller from alarm notification action in iOS? 如何在iPhone应用程序中为弹出视图控制器添加通知? - How to add a notification for pop view controller in iphone application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM