并根据选择的选项卡更改ViewController的内容和标题。

可能吗?

以下是我在UITabBarController的viewDidLoad函数中尝试过的方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSMutableArray * tabs = [[NSMutableArray alloc] init];

    EventsViewController * upcomingEvents = (EventsViewController *)[[self viewControllers] objectAtIndex:0];
    upcomingEvents.title = @"Upcoming Events";
    upcomingEvents.events = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];

    [tabs addObject:upcomingEvents];

    EventsViewController * myEvents = (EventsViewController *)[[self viewControllers] objectAtIndex:1];
    myEvents.title = @"My Events";
    myEvents.events = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"MyEvents" ofType:@"plist"]];

    [tabs addObject:myEvents];

    [self.tabBarController setViewControllers:tabs animated:YES];

}


这导致错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setEvents:]: unrecognized selector sent to instance 0x108fe3b50'


我当前的故事板设置


编辑:

最后得到一些帮助,并从Unkn0wn.Bit了解。

我废弃了自定义的UITabBarController,并刚刚在我的EventsViewController(UITableViewController)中调整了viewWillAppear函数,以根据tabBarController的selectedIndex更改源。

使用两个单独的NSMutableArray(myEvents,即将发生的事件),对多个tabBar按钮有效地使用同一视图(具有不同的信息)。

(void)viewWillAppear:(BOOL)animated {
    if([self.tabBarController selectedIndex]) {
        self.events = self.myEvents;
        self.navBar.title = @"My Events";
    } else {
        //temporary instead run function that either downloads events or tries to update if necessary
        self.events = self.upcomingEvents;
        self.navBar.title = @"Upcoming Events";
    }
}

最佳答案

如果您使用界面生成器,请在故事板文件中创建多个视图控制器(从Tab Bar Controller中按住ctrl +拖动以将它们分配给选项卡),然后根据需要设计视图,但是将其自定义类设置为同一类!

关于ios - 如何为每个标签使用使用相同ViewController的UITabBarController?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21681113/

10-11 22:23
查看更多