如何从导航控制器内的storyboard中的appdelegate推送viewcontroller

时间:2015-09-01 12:18:50

标签: ios objective-c uiviewcontroller appdelegate swrevealviewcontroller

我在我的项目中使用SWRevealViewController,我想在应用收到通知时打开一个特定的控制器。我尝试了很多解决方案,但没有任何效果。

我使用storyboard关注此http://www.appcoda.com/ios-programming-sidebar-navigation-menu/。我的故事板设计如下:

Storyboard

当应用程序收到通知时,我想在其导航控制器中加载照片视图控制器。我尝试使用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控制器,我需要的是使用故事板中已经定义的控制器,因为它具有特定的属性。

有什么想法吗?

由于

3 个答案:

答案 0 :(得分:6)

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

  }

答案 1 :(得分:2)

您可以使用以下方法

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

答案 2 :(得分:2)

请试试这可能会对你有所帮助。在此单击通知时,我们先调用此方法,然后调用appdelegate

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

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

[navController.visibleViewController.navigationController pushViewController:notificationViewController];    

}