【问题标题】:Dynamic NavigationController with UITabBar带有 UITabBar 的动态导航控制器
【发布时间】: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


    【解决方案1】:

    您应该为此使用情节提要。它将简化流程,让您直观地看到正在发生的事情。您的故事板将类似于 this

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

    【讨论】:

    • Thanx 但我更喜欢代码解决方案,我受不了可视化编辑器。我已经在调用 self.navigationController 并从中获取适当的大小值,但我的更新都不会影响导航栏。
    • 我的应用程序 UI 已更改,因此我不再需要解决此问题。但这可能与某处的故事板设置有关。该应用程序是一个混合故事板/直接代码应用程序,所以我可能可以通过更多故事板来解决它
    【解决方案2】:

    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;
    }
    

    【讨论】:

    • HomeController 是一个UIViewController,HomeTabController 是一个UITabBarController.. 抱歉名称令人困惑 - 我实际上已在我的代码中将 HomeController 重命名为 StoreController。
    • 我尝试了第一次编辑并在 HomeTabController 而不是 AppDelegate 中创建了 NavigationController,没有任何问题,但不再为我的任何视图调用 viewDidLoad
    猜你喜欢
    • 2017-05-14
    • 2011-03-29
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    相关资源
    最近更新 更多