【发布时间】:2015-01-02 19:58:01
【问题描述】:
我正在制作一个由 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 是只读的。
【问题讨论】:
标签: ios objective-c uitableview uitabbarcontroller uinavigationbar