【发布时间】:2014-02-11 00:35:20
【问题描述】:
并根据选择的选项卡更改 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 更改源。
使用两个单独的 NSMutableArrays(myEvents、comcomingEvents)有效地为多个 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";
}
}
【问题讨论】:
-
是的,多次实例化。你尝试了什么?什么没用?显示代码。
-
我发现这不是特别可靠,至少在iOS 8上是这样。 selectedIndex 的状态不会在正确的时间更新以进行一致的检测。
-
我发现如果你把逻辑放在 viewDidLoad 中是非常可靠的......!
标签: ios objective-c