iOS 点击TabController 某个item 不选中对应的viewController,执行自定义操作

本文档介绍了如何在iOS应用中,当用户点击底部TabBar的特定item时调起第三方SDK。首先,通过添加一个占位ViewController并设置其TabBarItem的图片和标题。接着,禁止单独的ViewController被选中状态。最后,重写TabBarController的`tabBar:didSelectItem:`方法来监听用户选择,并在选择“国是书院”时加载SDK。
摘要由CSDN通过智能技术生成

需求

点击 底部 tabbar 的 国是书院item的时候,调起第三方sdk

如图
点击底部item

请添加图片描述

调起三方sdk
请添加图片描述

实现

一 、添加 占位vc

- (void)showGssyVC
{
    TPGssyViewController *gssy = [[TPGssyViewController alloc] init];
    [self setUpOneViewController:gssy
                       WithImage:[Image(@"TabBar/tabbar_gssy_default") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                    WithSelImage:[Image(@"TabBar/tabbar_gssy_selected")imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                       WithTitle:@"国是书院"];
    self.gssyVC = gssy;
}

- (void)setUpOneViewController :(UIViewController *)Vc WithImage:(UIImage *)image WithSelImage:(UIImage *)selImage WithTitle:(NSString *)title{
    
    MLNavigationController *navC = [[MLNavigationController alloc]initWithRootViewController:Vc];
    navC.interactivePopGestureRecognizer.enabled = NO;
    Vc.tabBarItem.image = image;
    Vc.tabBarItem.selectedImage = selImage;
    Vc.tabBarItem.title = title;
    Vc.tabBarItem.imageInsets = UIEdgeInsetsMake(-3, 0, 3, 0);
    self.tabBar.tintColor = [UIColor colorWithHexString:@"0xB93034"];
}


二 、禁止占位vc可选中状体

实现 UITabBarControllerDelegate 的 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController API_AVAILABLE(ios(3.0))
方法

///当点击占位vc 的item时候,不选中相应的vc

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController API_AVAILABLE(ios(3.0))
{
    MLNavigationController *navi = (MLNavigationController *)viewController;
    if ([navi isKindOfClass:[MLNavigationController class]]) {
        if ([navi.viewControllers[0] isKindOfClass:[TPGssyViewController class]]) {
            return NO;;
        }
    }
  
    return YES;
}

三 、重写tabViewcontroller 的 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 方法

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSString *title = item.title;
    if ( if ([title isEqualToString:@"国是书院"]) {
        if (!self.zxtManager) {
            self.zxtManager = [zxtSDKManager getInstance];
        }
        ///调起三方sdk
        [self.zxtManager loadSDK];
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Flutter中,可以通过自定义Widget来创建一个自定义的Tab组件。首先,我们可以创建一个自定义的Tab类,继承自StatefulWidget,并实现它的build方法。 ```dart class CustomTab extends StatefulWidget { final String title; final bool isSelected; final Function onTap; CustomTab({required this.title, required this.isSelected, required this.onTap}); @override _CustomTabState createState() => _CustomTabState(); } class _CustomTabState extends State<CustomTab> { @override Widget build(BuildContext context) { return GestureDetector( onTap: widget.onTap, child: Container( color: widget.isSelected ? Colors.blue : Colors.transparent, child: Text( widget.title, style: TextStyle( fontSize: 16, color: widget.isSelected ? Colors.white : Colors.black, ), ), ), ); } } ``` 在这个自定义Tab类中,我们需要传入三个参数:title(标签的标题),isSelected(标签是否被选中),onTap(点击标签的回调方法)。 接下来,我们可以在TabBar中使用这个自定义Tab组件。 ```dart class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { late TabController _tabController; @override void initState() { super.initState(); _tabController = TabController(length: 3, vsync: this); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Custom Tab'), ), body: Column( children: [ TabBar( controller: _tabController, tabs: [ CustomTab( title: 'Tab 1', isSelected: _tabController.index == 0, onTap: () { _tabController.animateTo(0); }, ), CustomTab( title: 'Tab 2', isSelected: _tabController.index == 1, onTap: () { _tabController.animateTo(1); }, ), CustomTab( title: 'Tab 3', isSelected: _tabController.index == 2, onTap: () { _tabController.animateTo(2); }, ), ], ), Expanded( child: TabBarView( controller: _tabController, children: [ Center(child: Text('Content 1')), Center(child: Text('Content 2')), Center(child: Text('Content 3')), ], ), ), ], ), ); } } ``` 在这个例子中,我们使用TabBarTabBarView来显示标签和对应的内容。自定义的Tab组件被作为TabBar的child组件传入。TabBar接收一个TabController来管理标签的切换。每个自定义Tab组件通过传入isSelected参数来判断自身是否被选中,并通过onTap回调方法来触发点击事件并切换标签。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值