繁体   English   中英

如何在导航控制器内的storyboard中从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];

这可行,但创建了一个新的Navigtion控制器,我需要的是使用故事板中已经定义的控制器,因为它具有特定的属性。

任何想法?

谢谢

实际上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 

  }

您可以使用以下方法

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];

请尝试这可能会对你有所帮助。 在这个当你点击通知这个方法时我们先调用appdelegate

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{
UINavigationController *navController = (UINavigationController  *)self.window.rootViewController;

NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];

[navController.visibleViewController.navigationController pushViewController:notificationViewController];    

}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM