用UIButton 自定义UITabbarController+UINavgationController 实现页面跳转

//.h 文件
//成员变量定义:
@interface ZAAppDelegate : UIResponder <UIApplicationDelegate>
{
    
    UIView *tabbarView;
    UINavigationController *firstNV;
    UINavigationController *secondNV;
    UINavigationController *thirdNV;
}

//.m 文件
#import "ZAAppDelegate.h"
//此处的ZAFirstViewController.h,ZASecondViewController.h,ZAThirdViewController.h均为UIViewcontroller
#import "ZAFirstViewController.h"
#import "ZASecondViewController.h"
#import "ZAThirdViewController.h"
@implementation ZAAppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    
    //设置默认页面
    
    ZAFirstViewController *firstVC=[[ZAFirstViewController alloc]init];
    firstVC.title=@"1";
    firstNV=[[UINavigationController alloc]initWithRootViewController:firstVC];
    self.window.rootViewController=firstNV;
    
    [self.window makeKeyAndVisible];
    
    //初始化自定义uitabbar
    [self initTabbar];
    
    return YES;
}


//初始化tabbarview
-(void)initTabbar
{
    
    tabbarView=[[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.window.bounds)-40, 320, 40)];
    tabbarView.backgroundColor=[UIColor lightTextColor];
    tabbarView.userInteractionEnabled=YES;
    [self.window addSubview:tabbarView];
    
    for (int i=0; i<3; i++) {
        // 例子只是简单的写了按钮,实际运用中可以添加图片等自定义下导航风格
        
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(320/3*i, 0, 320/3, 40);
        [button setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        button.tag=i+1000;
        [button addTarget:self action:@selector(tabbarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [tabbarView addSubview:button];
        
        if (i==0) {
            
            button.selected=YES;
        }
    }
    
    
    
}

//  tabbar上的按钮点击事件
-(void)tabbarButtonClick:(UIButton *)button
{
    
    
    int tag;
    
    
    if (button.selected) {
        //button已经是选中状态,不执行任何操作
        return;
    }
    
    button.selected=!button.selected;
    
    //将除当前选中button的selected状态设为No
    for (UIButton *tabbutton in tabbarView.subviews) {
        
        
        if (tabbutton!=button) {
            tabbutton.selected=NO;
        }
    }
    
    
    tag=button.tag%10;
    
    
    //根据当前button的tag值 切换self.window的rootViewController,
    [self changeRootViewWithTag:tag];
}



//切换self.window的rootViewController,实现页面切换的效果
-(void)changeRootViewWithTag:(int)tag
{
    
    switch (tag) {
        case 0:
            if (!firstNV) {
                
                ZAFirstViewController *firstVC=[[ZAFirstViewController alloc]init];
                firstVC.title=@"1";
                firstNV=[[UINavigationController alloc]initWithRootViewController:firstVC];
                
            }
            
            self.window.rootViewController=firstNV;
            
            break;
        case 1:
            
            if (!secondNV) {
                
                ZASecondViewController *secondVC=[[ZASecondViewController alloc]init];
                secondVC.title=@"2";
                secondNV=[[UINavigationController alloc]initWithRootViewController:secondVC];
                
            }
            
            self.window.rootViewController=secondNV;
            break;
        case 2:
            
            if (!thirdNV) {
                
                ZAThirdViewController *thirdVC=[[ZAThirdViewController alloc]init];
                thirdVC.title=@"3";
                thirdNV=[[UINavigationController alloc]initWithRootViewController:thirdVC];
            }
            self.window.rootViewController=thirdNV;
            break;
        default:
            break;
    }
    
    
    // 将tabbarview 显示到最上层
    UIWindow *keyWindow=[[UIApplication sharedApplication] keyWindow];
    
    [keyWindow bringSubviewToFront:tabbarView];
    
    
}


 

                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值