iOS中用户未登录状态下点击下方tabBar触发登录

本文介绍了在iOS应用中,当用户未登录时点击TabBar会触发登录界面的实现方法。通过自定义`CustomTabBarController`,监听`UITabBarControllerDelegate`的`shouldSelectViewController`方法,检查用户登录状态。如果用户未登录且尝试进入特定页面(如社区或朋友),则弹出登录界面。同时展示了设置TabBarItem图片和文字的代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前提 公司没有设计访客视图界面  



效果图片

接下来直接上代码

@interface CustomTabBarController : UITabBarController<UITabBarDelegate,UITabBarControllerDelegate>


self.delegate = self;


//重点在这里

#pragma mark - 添加tabbaritem

- (void)addTabBarItems{

    

    UITabBar *bar = self.tabBar;

    

    UITabBarItem *homeItem = bar.items[0];

    [self setTabBarItem:homeItem image:@"sy_013" selectedImage:@"sy_017" title:@"摩点" tag:1];

    

    UITabBarItem *dynamicItem1 = bar.items[1];

    [self setTabBarItem:dynamicItem1 image:@"MD" selectedImage:@"MD2" title:@"项目" tag:2];

    

    UITabBarItem *dynamicItem = bar.items[2];

    [self setTabBarItem:dynamicItem  image:@"moxi" selectedImage:@"moxi2" title:@"社区" tag:3];

   

    

    UITabBarItem *inviteFriendsItem = bar.items[3];

    [self setTabBarItem:inviteFriendsItem image:@"sy_015" selectedImage:@"sy_019" title:@"朋友" tag:4];

    

    UITabBarItem *personalCenterItem = bar.items[4];

    [self setTabBarItem:personalCenterItem image:@"sy_016" selectedImage:@"sy_020" title:@"我的" tag:5];

    

    

    

}


- (void)setTabBarItem:(UITabBarItem *)item image:(NSString *)image selectedImage:(NSString *)selected title:(NSString *)title tag:(NSInteger)tag{

    

    item.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    

    item.selectedImage = [[UIImage imageNamed:selected] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    

    item.title = title;

    

    item.tag = tag;

    

    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateNormal];

    

    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: kColor(22, 180, 96)} forState:UIControlStateSelected];

}


#pragma mark - UITabBarControllerDelegate

//这个方法根据官方文档解释意思就是点击下面的tabBar的按钮时候  根据BOOL值来判断是否处于可继续点击状态

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0){

    

    if (viewController.tabBarItem.tag == 3 || viewController.tabBarItem.tag == 4){

        

        if (![Context logined]) {

        

            CKLoginVC *viewFlag = [[CKLoginVC alloc] init];

            

            UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewFlag];

            

            [self presentViewController:navi animated:YES completion:^{

                

            }];

            

            return NO;

            

        }else

        {

            

            return YES;

        }

    

    } else

    {

        

        return YES;

    }

    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值