ios - 点击UITabBarController的标签栏调用不同的viewControllers
问题描述
下面是我UITabBarController
在故事板中的结构图像。
现在在情节提要中,AboutUsViewController(UIViewController)
与我的 tabBar 按钮单击事件绑定,即如果我单击 tabBar 按钮,AboutUsViewController
则正在打开,但现在我的功能基于某些条件。我想打电话ContactRequstViewController
而不是AboutUsViewController
在同一个 tabBar 按钮上单击。
以下是我打开的代码ContactRequstViewController
。
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if (tabBarController.selectedIndex == 2){
UIStoryboard *story = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];
ContactRequstViewController *contactVC = [story instantiateViewControllerWithIdentifier:@"ContactUsView"];
[self.navigationController pushViewController:contactVC animated:YES];
}
}
写完上面的代码后,我无法加载ContactRequestViewController
.
解决方案
如果要根据自定义逻辑替换所选选项卡上的根视图控制器,请尝试setViewControllers:animated:
使用UITabBarController
.
你可以这样做:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 1 && tabBarController.viewControllers.count > tabBarController.selectedIndex) {
BOOL shouldShowContactVC = (BOOL)(rand() % 2);
NSMutableArray *viewControllers = [[tabBarController viewControllers] mutableCopy];
UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *newVC = nil;
if (shouldShowContactVC) {
newVC = [main instantiateViewControllerWithIdentifier:@"ContactUsVC"];
} else {
newVC = [main instantiateViewControllerWithIdentifier:@"AboutUsVC"];
}
if (newVC) {
[viewControllers replaceObjectAtIndex:tabBarController.selectedIndex withObject:newVC];
newVC.tabBarItem = viewController.tabBarItem;
[tabBarController setViewControllers:viewControllers animated:YES];
}
}
}
推荐阅读
- python - 打印出列表中某个范围内的最大值/最小值
- amazon-web-services - 使用 Flask Web 应用程序配置 Route53、ALB 和 Fargate
- c# - asp.net Webforms:如何从一个页面发送 POST 请求并在不同页面上读取 POST 响应
- r - 错误:将绘图输出到 PDF 时字体类型无效
- xamarin.forms - 通过调用委托加载自定义 WebView - Xamarin (iOS)
- python - 使用烧瓶在邮递员中返回的重复字段
- flutter - 使用 Google Maps Flutter Plugin 在 Android 上崩溃的 Flutter App
- max-msp-jitter - Max/MSP:如何操作 zl 列表数据以获取上四分位数
- go - 如何同时使用 LoadHTMLGlob 和 LoadHTMLFiles
- android - 通过 Android Studio 中的 ADM 下载管理器下载