[英]How do I use a UITabBarController using the same ViewController for every tab?
and change the content and title of the ViewController depending on which tab is selected. 并根据选择的选项卡更改ViewController的内容和标题。
Is it possible? 可能吗?
Below is what I tried within my UITabBarController's viewDidLoad function 以下是我在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];
}
This results in the error 这导致错误
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setEvents:]: unrecognized selector sent to instance 0x108fe3b50'
My current storyboard setup 我当前的故事板设置
EDIT: 编辑:
In the end with some help and understand from Unkn0wn.Bit. 最后得到一些帮助,并从Unkn0wn.Bit了解。
I scrapped the custom UITabBarController and just adjusted my viewWillAppear function in my EventsViewController (UITableViewController) to change the source depending on the selectedIndex of the tabBarController. 我废弃了自定义的UITabBarController,并刚刚在我的EventsViewController(UITableViewController)中调整了viewWillAppear函数,以根据tabBarController的selectedIndex更改源。
Effectively using the same view (with different information) for multiple tabBar buttons using two separate NSMutableArrays (myEvents, upcomingEvents). 使用两个单独的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";
}
}
This part of the error message: 错误消息的这一部分:
-[UINavigationController setEvents:]
-[UINavigationController setEvents:]
tells you that you are calling setEvents:
on an instance of UINavigationController
and it doesn't respond. 告诉您您正在
UINavigationController
的实例上调用setEvents:
并且不响应。
Your issue is that you are forgetting your view controller hierarchy because you have your EventsViewController
instances inside navigation controllers so you need to traverse the hierarchy to call the correct instances. 您的问题是您忘记了视图控制器层次结构,因为在导航控制器内部有
EventsViewController
实例,因此您需要遍历层次结构以调用正确的实例。
ie: 即:
UINavigationController *navController = (UINavigationController *)[[self viewControllers] objectAtIndex:0];
EventsViewController *upcomingEvents = [navController topViewController];
如果您使用界面生成器,请在故事板文件中创建多个视图控制器(从Tab Bar Controller中按住ctrl +拖动以将其分配给选项卡),然后根据需要设计视图,但是将其自定义类设置为同一类!
You could use multiple instances of your ViewController
subclass, and that would give you what you want. 您可以使用
ViewController
子类的多个实例,这将为您提供所需的内容。 But if you wanted to simply use the exact same instance for each, then you would want to use a UITabBar
directly, not a UITabBarController
. 但是,如果您只想为每个实例使用完全相同的实例,那么您将需要直接使用
UITabBar
而不是UITabBarController
。 You could add the UITabBar
to your UIViewController
subclass and implement the UITabBarDelegate
protocol. 您可以将
UITabBar
添加到UIViewController
子类中,并实现UITabBarDelegate
协议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.