带 UITabBar 的动态 NavigationController

Dynamic NavigationController with UITabBar

我正在制作一个由 UITabBarController 控制的应用程序,我想在每个视图中显示不同的导航栏。问题是我的更新没有影响。

在 AppDelegate 中,我有这段代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

HomeTabController *vc = [[HomeTabController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:vc];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];

return YES;
}

这是我的 HomeTabController,它扩展了 UITabBarController:

- (void)viewDidLoad {
[super viewDidLoad];

// FirstViewController
HomeController *fvc=[[HomeController alloc]initWithNibName:nil bundle:nil];
fvc.title=@"Store";
fvc.tabBarItem.image = [self resize_image:@"Bank" newSize:CGSizeMake(30, 30)];

//SecondViewController
ViewProfileController *svc=[[ViewProfileController alloc]initWithNibName:nil bundle:nil];
svc.title=@"Profile";
svc.tabBarItem.image = [self resize_image:@"Contacts" newSize:CGSizeMake(30, 30)];

self.viewControllers = [NSArray arrayWithObjects:fvc, svc, nil];

}

这是 ViewProfileController:

- (void)viewDidLoad {
[super viewDidLoad];

self.title = @"Profile";

CGRect screenRect = [[UIScreen mainScreen] bounds];
sw = screenRect.size.width;
sh = screenRect.size.height;

nh = self.navigationController.navigationBar.frame.size.height;
ny = nh + self.navigationController.navigationBar.frame.origin.y;

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,ny,sw,sh-ny) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.backgroundView = nil;
[self.tableView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:self.tableView];
}

-(void)viewWillAppear:(BOOL)animated
{
UIBarButtonItem *edit_btn = [[UIBarButtonItem alloc]
                             initWithTitle:@"Edit"
                             style:UIBarButtonItemStyleDone
                             target:self
                             action:@selector(nav_edit_profile)];
self.navigationItem.rightBarButtonItem = edit_btn;
}

当我 运行 应用程序时,标签栏创建正确,我可以在两个视图之间导航而不会出错。但是,NavigationBar 是空的,没有标题或编辑按钮。

如果此视图在 AppDelegate 而非 HomeTabController 中进行初始控制,则代码可以正常工作。

如何在使用 UITabBarController 时编辑导航栏?

我想我可能只需要设置视图参考:fvc.navigationController = self.navigationController; 但它说 navigationController 是只读的。

UITabbarController 未设计为位于 UINavigationController 内。所以你的代码应该如下:

self.window.rootViewController = yourTabbarControllr;

你为什么要加载这个视图:

HomeController *fvc=[[HomeController alloc]initWithNibName:nil bundle:nil];

您要在当前标签栏旁边创建另一个标签栏吗?

编辑: 这是一个例子:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    HomeTabController *tabController = [[HomeTabController alloc] init];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.myViewController1 = [[myViewController1 alloc] init];
    self.myViewController2 = [[myViewController2 alloc] init];
    self.myViewController3 = [[myViewController3 alloc] init];

    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:self.myViewController1];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:self.myViewController2];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:self.myViewController3];

    tabController.viewControllers = @[nav1, nav2, nav3];
    self.window.rootViewController = tabController;
    [self.window makeKeyAndVisible];

return YES;
}

您应该为此使用故事板。它将简化流程,让您直观地看到发生了什么。你的故事板看起来像 this.

那么,您在AppDelegate中根本不需要添加任何代码,就可以正常使用ViewControllers了。如果要从该视图控制器调用特定视图控制器的导航控制器,请调用 self.navigationController.