如何在导航控制器内的情节提要中从 appdelegate 推送 viewcontroller
How to push viewcontroller from appdelegate in storyboard inside navigation controller
我在我的项目中使用 SWRevealViewController,我想在应用程序收到通知时打开一个特定的控制器。我尝试了很多解决方案,但没有任何效果。
我通过使用情节提要来遵循此 http://www.appcoda.com/ios-programming-sidebar-navigation-menu/。我的故事板设计如下:
当应用程序收到通知时,我想在其导航控制器中加载照片视图控制器。我在 AppDelegate 中尝试使用以下代码:
UIStoryboard *st = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
photoViewController *descController = (PhotoViewController*)[st instantiateViewControllerWithIdentifier: @"photoView"];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:descController];
SidebarTableViewController *rearViewController = (SidebarTableViewController*)[st instantiateViewControllerWithIdentifier: @"menuController"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc] init];
mainRevealController.rearViewController = rearViewController;
mainRevealController.frontViewController= frontNavigationController;
self.window.rootViewController =nil;
self.window.rootViewController = mainRevealController;
[self.window makeKeyAndVisible];
这可行,但创建了一个新的导航控制器,我需要的是使用故事板中已经定义的那个,因为它具有特定的属性。
有什么想法吗?
谢谢
您可以使用以下方法
NSString *storyBoardName=@"Main_iPad";
if (isPhone)
storyBoardName=@"Main_iPhone";
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyBoardName bundle:nil];
PlayVideoBySocketsScene *controller=[storyBoard instantiateViewControllerWithIdentifier:@"playVideoUsingSockets"];
float disptachTime=2.0;
if (value)
{
disptachTime=1.0;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(disptachTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:controller animated:YES completion:nil];
实际上 SwLRevalViewController
取得了 Root
的所有权,所以这样做
在Appdelegate.m
//initially navigate to your Main controller on SWL
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
NSString *alert = userInfo[@"aps"][@"redirect_url"];
[[NSUserDefaults standardUserDefaults]setObject:alert forKey:@"itemType"];
[[NSUserDefaults standardUserDefaults]synchronize];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
SWRevealViewController *main = (SWRevealViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"SWRevealViewController"];
self.window.rootViewController = main;
}
//它会自动重定向到你的根控制器
在该主视图控制器上 ViewDidLoad
检查/导航到您的照片视图控制器
NSString *getType=[[NSUserDefaults standardUserDefaults]objectForKey:@"itemType"];
if ([gettypeofItem isEqualToString:@"something"])
{
yourPhotoViewController *ivc=[self.storyboard instantiateViewControllerWithIdentifier:@"yourPhotoViewController"];
[self.navigationController pushViewController:ivc animated:YES];
}
else
{
// do nothing
}
请试试这个,它可能对你有帮助。在这种情况下,当您单击通知时,我们首先为 appdelegate
调用了该方法
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];
[navController.visibleViewController.navigationController pushViewController:notificationViewController];
}
我在我的项目中使用 SWRevealViewController,我想在应用程序收到通知时打开一个特定的控制器。我尝试了很多解决方案,但没有任何效果。
我通过使用情节提要来遵循此 http://www.appcoda.com/ios-programming-sidebar-navigation-menu/。我的故事板设计如下:
当应用程序收到通知时,我想在其导航控制器中加载照片视图控制器。我在 AppDelegate 中尝试使用以下代码:
UIStoryboard *st = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
photoViewController *descController = (PhotoViewController*)[st instantiateViewControllerWithIdentifier: @"photoView"];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:descController];
SidebarTableViewController *rearViewController = (SidebarTableViewController*)[st instantiateViewControllerWithIdentifier: @"menuController"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc] init];
mainRevealController.rearViewController = rearViewController;
mainRevealController.frontViewController= frontNavigationController;
self.window.rootViewController =nil;
self.window.rootViewController = mainRevealController;
[self.window makeKeyAndVisible];
这可行,但创建了一个新的导航控制器,我需要的是使用故事板中已经定义的那个,因为它具有特定的属性。
有什么想法吗?
谢谢
您可以使用以下方法
NSString *storyBoardName=@"Main_iPad";
if (isPhone)
storyBoardName=@"Main_iPhone";
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyBoardName bundle:nil];
PlayVideoBySocketsScene *controller=[storyBoard instantiateViewControllerWithIdentifier:@"playVideoUsingSockets"];
float disptachTime=2.0;
if (value)
{
disptachTime=1.0;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(disptachTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:controller animated:YES completion:nil];
实际上 SwLRevalViewController
取得了 Root
的所有权,所以这样做
在Appdelegate.m
//initially navigate to your Main controller on SWL
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
NSString *alert = userInfo[@"aps"][@"redirect_url"];
[[NSUserDefaults standardUserDefaults]setObject:alert forKey:@"itemType"];
[[NSUserDefaults standardUserDefaults]synchronize];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
SWRevealViewController *main = (SWRevealViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"SWRevealViewController"];
self.window.rootViewController = main;
}
//它会自动重定向到你的根控制器
在该主视图控制器上 ViewDidLoad
检查/导航到您的照片视图控制器
NSString *getType=[[NSUserDefaults standardUserDefaults]objectForKey:@"itemType"];
if ([gettypeofItem isEqualToString:@"something"])
{
yourPhotoViewController *ivc=[self.storyboard instantiateViewControllerWithIdentifier:@"yourPhotoViewController"];
[self.navigationController pushViewController:ivc animated:YES];
}
else
{
// do nothing
}
请试试这个,它可能对你有帮助。在这种情况下,当您单击通知时,我们首先为 appdelegate
调用了该方法-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];
[navController.visibleViewController.navigationController pushViewController:notificationViewController];
}