IOS开发控件视图day07:纯代码设置TabBar及item转跳方式

   日期:2020-07-18     浏览:102    评论:0    
核心提示:本章主要介绍如何新建一个带有三个子页面的TabBar的项目,并且第三个的个人信息界面判断未登录时转跳到登录页面1、SceneDelegate设置主启动(1)SceneDelegate.h中声明UIWindow@property (strong, nonatomic) UIWindow * window;(2)SceneDelegate.m中设置启动页及Navigation- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSes

本章主要介绍如何新建一个带有三个子页面的TabBar的项目,并且第三个的个人信息界面判断未登录时转跳到登录页面
1、SceneDelegate设置主启动
(1)SceneDelegate.h中声明UIWindow

@property (strong, nonatomic) UIWindow * window;

(2)SceneDelegate.m中设置启动页及Navigation

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
	//初始化目标页面对象
    TBViewController *tabbarcon = [[TBViewController alloc]init]; 
    //设置Navigation
    UINavigationController *rootnav = [[UINavigationController alloc]initWithRootViewController:tabbarcon];
    rootnav.navigationBarHidden = YES;
    self.window.rootViewController = rootnav;
    [self.window makeKeyAndVisible];
}

2、New文件
(1)新建一个继承于UITabBarController的类文件TBViewController
(2)新建三个TabBar的子页面,分别为FirstViewController、SecondViewController、ThirdViewController,以及ThirdViewController转跳到的登录界面LoginViewController
(3)新建一个继承于NSObject的Uitl文件,设置一个单例监听用户登录状态

3、TBViewController中设置子视图
(1)导入子页面头文件,声明全局对象

#import "TBViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "LoginViewController.h"
#import "Uitl.h"
@interface TBViewController ()
@property(nonatomic,strong)FirstViewController *temp1;
@property(nonatomic,strong)SecondViewController *temp2;
@property(nonatomic,strong)ThirdViewController *temp3;
@property(nonatomic,strong)LoginViewController *temp4;
@property(nonatomic,strong)NSMutableArray *controllerArray;
@end

(2)构造方法设置子页面Item

@implementation TBViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewControllers];    
}

- (NSMutableArray *)controllerArray{
    if (!_controllerArray) {
        _controllerArray = [NSMutableArray new];
    }
    return _controllerArray;
}
//设置子页面属性(方法一)
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title  imageeName:(NSString *)imageName selectImageName:(NSString *)selectImageName tag:(NSInteger)tag{
    childController.tabBarItem.title = title;
    childController.tabBarItem.image = [UIImage imageNamed:imageName];
    childController.tabBarItem.selectedImage = [UIImage imageNamed:selectImageName];
    childController.tabBarItem.tag = tag;
    [self.controllerArray addObject:[[UINavigationController alloc]initWithRootViewController:childController]];
}
//设置Item(方法一)
-(void)initViewControllers
{
	//初始化类对象
    _temp1 = [[FirstViewController alloc]init];
    //设置Item标题,选中和未选中状态下的图标以及tag值
    [self addChildViewController:_temp1 title:@"首页" imageeName:@"首页" selectImageName:@"首页sel" tag:0];
    _temp2 = [[SecondViewController alloc]init];
    [self addChildViewController:_temp2 title:@"次页" imageeName:@"次页" selectImageName:@"次页sel" tag:1];
    _temp3 = [[ThirdViewController alloc]init];
    [self addChildViewController:_temp3 title:@"我的" imageeName:@"我的" selectImageName:@"我的sel" tag:2];
    
    self.viewControllers = self.controllerArray;
    self.selectedIndex = 0;//设置默认启动页为第一页,即首页
}
//选中第三个子页面时,设置未登录转跳
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item.tag == 2) {    
        if ([Uitl share].isLogin) { 	//判断用户是否登录
            return;
        }
        _temp4 = [[LoginController alloc]init];
        [_temp4 setLoginResultBlock:^(BOOL flag,id _Nonnull loginInfo){   
            if (flag) {
            }else{
                self.selectedIndex = 0;
            }
        }];
        [self.navigationController pushViewController:_temp4 animated:YES];
        NSLog(@"didSelectItem");
    }   
}
@end

(3)设置Item方法二
例:SecondViewController.m

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:@"SecondViewController" bundle:nil];
    if (self) {
        UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:@"次页" image:[UIImage imageNamed:@"次页"] selectedImage:[UIImage imageNamed:@"次页sel"]];
        item2.tag = 2;
        self.tabBarItem = item2;
    }
    return self;

4、设置Uitl文件
(1)Uitl.h

@property (nonatomic,assign) BOOL isLogin;
+(PCDUil *)share;

(2)Uitl.m

+(PCDUil *)share
{
    static PCDUil *pcdutil;
    static dispatch_once_t once;
    dispatch_once(&once,^{
        pcdutil = [[PCDUil alloc]init];
    });
    return pcdutil;
}

5、LoginViewController页面设置登录与返回按钮
LoginViewController.h

@property (nonatomic,copy)void(^LoginResultBlock)(BOOL flag,id loginInfo);

LoginViewController.m

//返回首页按钮触发函数
-(void)back:(UIBarButtonItem *)buttonItem
{
    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"back");
    if (self.LoginResultBlock) {
        self.LoginResultBlock(NO,nil);
    }  
}
//登录按钮
-(void)login:(UIButton *)btn
{
	LoginViewController *login = [[LoginViewController alloc]init];
	[Uitl share].isLogin = YES;	//设置状态为登录
   	[login.navigationController popViewControllerAnimated:YES];
}

6、在首页设置一个按钮btn,触发函数为setIsLogin,监听用户登录状态
FirstViewController.m

//点击按钮触发的函数
-(void)setIsLogin:(UIButton *)btn
{
    if ([Uitl share].isLogin) {
        [Uitl share].isLogin = NO;
        [_btn setTitle:@"您未登录,点击登录" forState:UIControlStateNormal];
        _btn.backgroundColor = [UIColor redColor]; 
    }else{
        LoginViewController *tmp4 = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:tmp4 animated:YES];
    }
}
//进入首页时,按钮显示的状态
-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([Uitl share].isLogin) {
        [_btn setTitle:@"您已登录,点击退出" forState:UIControlStateNormal];
        _btn.backgroundColor = [UIColor greenColor];
    }else{
        [_btn setTitle:@"您未登录,点击登录" forState:UIControlStateNormal];
        _btn.backgroundColor = [UIColor redColor];
    }
}
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服