【发布时间】:2022-01-02 19:12:38
【问题描述】:
我对这个项目的目标是在使用 UITabBar 的视图控制器之间循环,而不使用 UITabBarController,因为根据 Apple 文档,不应将 TabBarControllers 推送到该项目已经使用的 UINavigationControllers。
到目前为止,我使用的是@samuel 对此问题的回答中的UITabBar。 How to add UITabBar in iphone using objective c
//viewDidLoad
...
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 431, 320, 50)];
[self.view addSubview:tabBar];
[[UITabBar appearance] setBarTintColor:[UIColor blackColor]];
NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed:@""] tag:0];
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Rules" image:[UIImage imageNamed:@""] tag:1];
[tabBarItems addObject:tabBarItem];
[tabBarItems addObject:tabBarItem1];
tabBar.items = tabBarItems;
tabBar.selectedItem = [tabBarItems objectAtIndex:0];
}
当我按下 tabBarItems 时,我已准备好在视图控制器之间循环。这样做的正确方法是什么?我想在点击Rules tabBarItem 时转到名为ViewController9 的视图控制器,然后在点击Home tabBarItem 时转到ViewController6。
有人可以分享一些代码吗?这是我尝试过的,但按下 tabBarItems 时没有任何反应:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSInteger selectedTag = tabBar.selectedItem.tag;
NSLog(@"%ld",(long)selectedTag);
if (selectedTag == 0) {
//Do what ever you want here
NSString * storyboardName = @"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController9"];
[self presentViewController:vc animated:YES completion:nil];
} else if(selectedTag == 1) {
NSString * storyboardName = @"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController9"];
[self presentViewController:vc animated:YES completion:nil];
} else { //if(selectedTag == 2)
//Do what ever you want here
}
}
【问题讨论】:
标签: ios objective-c