2
- (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 屏幕中可见?

4

2 回答 2

1

进入 AppDelegate.h 文件制作 TabBarController 的属性:

@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;
}
于 2013-11-09T03:08:42.797 回答
0

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

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

文档

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

尝试删除该行,并根据@rmaddy 的建议,将每个视图控制器放入导航控制器中。然后将这些 Navigation 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];
于 2013-11-09T01:46:44.243 回答