在UIViewController上以rootView身份设置UINavigation Controller

时间:2012-04-09 22:42:49

标签: ios uiviewcontroller uitabbarcontroller

我有一个tabBarApplication,我有一个模板,每个标签上都有一个UINavigation模板。

我想使用该示例(排序)并将其转换为其他应用程序中的单个UIViewController。我提出了两段代码,第一段是我的模板,后者是我想要的。有人可以给我一些提示或帮助解决这个问题吗?我不断收到错误,但它们对我没有意义,因为tabBarApp不需要viewController声明它。

第一个代码(示例):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setupFetchedResultsController];

if (![[self.fetchedResultsController fetchedObjects] count] > 0 ) {
    NSLog(@"!!!!! ~~> There's nothing in the database so defaults will be inserted");
    [self importCoreDataDefaultRoles];
}
else {
    NSLog(@"There's stuff in the database so skipping the import of default data");
}

// The Tab Bar
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

// The Two Navigation Controllers attached to the Tab Bar (At Tab Bar Indexes 0 and 1)
UINavigationController *personsTVCnav = [[tabBarController viewControllers] objectAtIndex:0];
UINavigationController *rolesTVCnav = [[tabBarController viewControllers] objectAtIndex:1];
return YES;
}

第二个代码(我想要做的):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setupFetchedResultsController];

if (![[self.fetchedResultsController fetchedObjects] count] > 0 ) {
    NSLog(@"!!!!! ~~> There's nothing in the database so defaults will be inserted");
    [self importCoreDataDefaultRoles];
}
else {

UIViewController *mainViewController = (UIViewController *)self.window.rootViewController;

UINavigationController *readingsTVCnav = [[mainViewController viewController] objectAtIndex:0];

// Override point for customization after application launch.
return YES;
}

代码的提取部分与已经设置和工作的Core Data相关。 这种改变的原因是我希望将普通视图控制器设置为初始屏幕而不是tabBarConfiguration。

干杯杰夫

编辑:为了清晰起见,我添加了一张图片

enter image description here

1 个答案:

答案 0 :(得分:1)

根据您在图像中描述和描绘的内容,您可以将导航控制器设置为应用程序的根视图控制器。因此,您可以在应用委托的didFinishLaunchingWithOptions:方法中访问它(如果需要),如下所示:

UINavigationController *navController = (UINavigationController *)self.window.rootViewController;

虽然对于您的应用程序,我认为您没有任何理由需要从您的应用代表中引用导航控制器。您需要的所有设置(除了代码数据代码,您说已经在工作)都可以通过故事板处理。

在你的nagivationController的root viewController(带有所有按钮的那个)中,你应该在每个按钮中设置一个segue到storyboard中的approperiate viewController。确保将其设置为“push”segues,以便将视图控制器推送到navigationController的导航堆栈。如果在segue发生时你需要为子视图控制器做任何特定的设置,你可以像这样实现prepareForSegue:方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowMyViewController"]) {

        MyViewController *vc = (MyViewController*)segue.destinationViewController;       
        vc.title = @"My Title";
        vc.someProperty = @"Some Value";
    }
}

您可以(并且应该)使用唯一标识符识别故事板中的每个segue,以便在调用此方法时识别它们。

相关问题