我正在构建一个iOS应用程序。我面临的问题是,如何在应用程序委派中呈现特定视图并维护导航堆栈?
现在发生的事情是,点击推送通知,它会把我带到初始ViewController。
我需要的是点击推送通知,它应该会把我带到那个特定的ViewController。
假设有四个视图控制器:A、B、C、D。我需要的第一件事是点击推送通知它应该会把我带到D视图控制器,当我按下该视图的后退按钮时,我可以移动到C视图viceVersa B.
请帮帮我。我是iOS新手。
在您的AppDelegate中,在didFinishLaunchingWithOptions方法中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if( launchOptions )
{
if([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey])
{
// Push your viewcontroller here
}
}
如果你的应用程序启动或处于后台,请将你的视图控制器推入:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
对于你的堆栈,类似的东西
ControllerB* B = [[ControllerB alloc] initWithNibName:"ControllerB" bundle:nil];
[A presentViewController:B animated:NO completion:^{
ControllerC* C = [[ControllerC alloc] initWithNibName:"ControllerC" bundle:nil];
[B presentViewController:C animated:NO completion:^{
}];
}];
@ejanowski接近正确答案,但OP特别谈到了"导航堆栈",这意味着UINavigationController
。调用presentViewController:animated:completion:
将以模式显示视图控制器(无论如何都很糟糕)。
在UINavigationController
上有一个方便的方法叫做setViewControllers:animated:
。你需要使用:
if (appOpenedByPushNotification) {
UIViewController* A = ... ;
UIViewController* B = ... ;
UIViewController* C = ... ;
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:A]; // you might be able to just do [UINavigationController new] but I've never tried that.
[nav setViewControllers:@[A, B, C] animated:NO];
// show the nav however you want, maybe like this:
self.window.rootViewController = nav;
}
Yup应该添加以下代码,当用户单击didFinishLaunchingWithOptions
的通知时启动
if ([[launchOptions allKeys] containsObject:UIApplicationLaunchOptionsRemoteNotificationKey]) {
id userInfo=[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSLog(@"%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
//Open your view controller here
}
}
您需要从通知深度链接到要呈现的ViewController。如果没有深度链接,应用程序就不知道将用户放在导航堆栈的何处。在通知中,包括一个值,该值指示哪个视图应在委派中显示和处理演示。这是一个简单的例子。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSDictionary *pushNotification = [options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushNotification )
{
//Check the notification to see which view you want to present and handle it here
}
}
在AppDelegate.m中添加以下方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *viewControllerB = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerBIdentifier"];
self.window.rootViewController = viewControllerB;
}
您可以使用UINavigationController的setViewControllers:animated:
方法您需要实例化相关的VC,用任何相关的数据/状态初始化它们,然后用它们按照从根视图控制器到顶视图控制器的顺序构建一个数组:
NSArray *newStack = @[rootVC, ..., page11VC, page12VC, page13VC];
[self.navigationController setViewControllers:newStack animated:YES];
然后从page13VC弹出将转到page12VC等。您可以使用导航控制器的viewControllers
属性获取rootVC和任何其他预先存在的VC。
iOS-如何推送到视图控制器并维护正确的";视图堆栈";返回时
如何从navigationController(viewControllers/stack)返回指定viewController?