当我从UINavigationController推送新视图时,UITabBarController不会执行

时间:2013-11-09 01:27:28

标签: objective-c uitabbarcontroller

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:   (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.


FirstViewController *fvc = [[FirstViewController alloc] init];
SecondtViewController *svc = [[FirstViewController alloc] init];


//Create UITabBarController
UITabBarController *theTabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSSArry arrayWithObjects: fvc, svc, nil];
[theTabBarController setViewControllers:viewControllers];


// Create UINavigationController
UINavigationController *theNavigationController = [[UINavigationController         
alloc]initWithRootViewController:theTabBarController];
[[self window] setRootViewController:theNavigationController];


self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}

然后在第一视图控制器中,我推送到第二个视图

- (IBAction)Page2:(id)sender {
SBHomePageDetailViewController *detailPageViewController =   [[SBHomePageDetailViewController alloc] init];
// Pushing to the stack

[[self navigationController] pushViewController:detailPageViewController animated:YES];
}

现在我的UI显示了第二个视图,但是缺少UITabBarController。当我向后导航时,标签栏视图又回来了。如何在所有ui屏幕中显示标签栏控制器?

2 个答案:

答案 0 :(得分:1)

进入AppDelegate.h文件makeTabBarController的属性:

@property (nonatomic, strong) UITabBarController *theTabBarController;

这是我改变你的didFinishLaunchingWithOptions方法的方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.


    FirstViewController *fvc = [[FirstViewController alloc] init];
    SecondtViewController *svc = [[SecondtViewController alloc] init];


    // Create UINavigationController
    UINavigationController *theNavigationController = [[UINavigationController
                                                        alloc]initWithRootViewController:fvc];

    //Create UITabBarController
    self.theTabBarController = [[UITabBarController alloc] init];
    NSArray *viewControllers = [NSArray arrayWithObjects: theNavigationController, svc, nil];

    [self.theTabBarController setViewControllers:viewControllers];

    [[self window] setRootViewController:theNavigationController];
    [[self window] addSubview:self.theTabBarController.view];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

答案 1 :(得分:0)

代码中的问题是它尝试将UITabBarController初始化为此行中UINavigationController的rootViewController:

// Create UINavigationController
UINavigationController *theNavigationController = [[UINavigationController alloc]initWithRootViewController:theTabBarController];

来自docs

  

RootViewController的:   位于导航堆栈底部的视图控制器。该对象不能是UITabBarController类的实例。

尝试删除该行,并根据@ rmaddy的建议,将每个View Controller放入导航控制器中。然后将这些导航控制器设置为Tab Bar Controller的VC并将App的RootViewController设置为Tab Bar Controller:

FirstViewController *fvc = [[FirstViewController alloc] init];
SecondtViewController *svc = [[SecondtViewController alloc] init];


// Create the first UINavigationController
UINavigationController *firstNavigationController = [[UINavigationController
                                                    alloc]initWithRootViewController:fvc];

// Create the second UINavigationController
UINavigationController *secondNavigationController = [[UINavigationController
                                                    alloc]initWithRootViewController:svc];

//Create UITabBarController
theTabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects: firstNavigationController, secondNavigationController, nil];
[theTabBarController setViewControllers:viewControllers];

[[self window] setRootViewController: theTabBarController];