除了通过app委托获得rootViewController还有其他选择

时间:2015-11-25 19:57:32

标签: ios objective-c instance multiple-instances rootview

我正在开发一个深度链接应用程序,我需要将我的深层链接管理器的委托分配给我的tabbarcontroller的子类

如何从标签栏控制器的子类内部返回根tabbarcontroller?

这是app del函数,我调用[TMDeeplinkManager searchForPodcast ...] [self.mainTabController getMainTabBarController]我想将其更改为

[TMMainTabBarController mainTabBarController]

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

    NSNumber *collectionId = [url host];
    NSString *episodeTitle = [NSString stringWithFormat:@"%@", [url lastPathComponent]];

    [TMDeeplinkManager searchForPodcastWithCollectionID:collectionId
                                                  title:episodeTitle
                                            andDelegate:[self.mainTabController getMainTabBarController]];

    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
}

这是tabbarcontroller的子类,如何在instancetype中返回根视图控制器?没有使用app del?

@interface TMMainTabBarController () <UINavigationControllerDelegate>

@property (strong, nonatomic) id selectedItem;

@end

@implementation TMMainTabBarController {
    TMMainTabBarController *mainTabBarController;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)didSelectEpisode:(TMPodcastEpisode *)episode {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil] ;
    TMAudioPlayerViewController *audioPlayerViewController = [storyboard instantiateViewControllerWithIdentifier:@"TMAudioPlayerViewController"];

    audioPlayerViewController.episode = episode;

    UINavigationController *mainNavController = self.viewControllers[0];
    [mainNavController pushViewController:audioPlayerViewController animated:true];

}

-(void)setMainTabBarController:(TMMainTabBarController *)tabBarController {
    mainTabBarController = tabBarController;
}

-(TMMainTabBarController *)getMainTabBarController {
    return mainTabBarController;
}

+(instancetype)mainTabBarController {

    return self.mainTabBarController;
}

@end

1 个答案:

答案 0 :(得分:1)

UIViewController *root = self;
while (root.parentViewController != nil) {
    root = root.parentViewController;
}

//中提琴!