程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 自定義UITabBarController

自定義UITabBarController

編輯:C++入門知識

AppDelegate:

#import "AppDelegate.h"
#import "BaseTabBarController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    BaseTabBarController *uiTabBarController =[[BaseTabBarController alloc]initWithNibName:nil bundle:nil];
    OneViewController *oneC=[[OneViewController alloc] initWithNibName:@"OneViewController" bundle:nil];
    TwoViewController *twoC=[[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:nil];
    ThreeViewController *threeC=[[ThreeViewController alloc] initWithNibName:@"ThreeViewController" bundle:nil];
    NSArray *arrVC=[NSArray arrayWithObjects:oneC,twoC,threeC, nil];
    [uiTabBarController setViewControllers:arrVC];
    [uiTabBarController showItems];
    self.window.rootViewController=uiTabBarController;
    return YES;
}

BaseTabBarController.h:

#import 

@interface BaseTabBarController : UITabBarController
- (void)onTabButtonClik:(id)sender;
- (void)showItems;
BaseTabBarController.m:

//
//  BaseTabBarController.m
//  tabbar
//
//  Created by dong on 14-5-20.
//  Copyright (c) 2014年 dong. All rights reserved.
//

#import "BaseTabBarController.h"

@interface BaseTabBarController ()

@end

@implementation BaseTabBarController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)showItems{
    for (int i=0; i<3; i++) {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(i*109, 0, 109, 50)];
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"normal%d.png",i+1]] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"press%d.png",i+1]] forState:UIControlStateSelected];
//        [btn setBackgroundImage:[UIImage imageNamed:@"mineCirclePress.png"] forState:UIControlStateHighlighted];
        [btn setTag:i+1];
        [btn addTarget:self action:@selector(onTabButtonClik:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:btn];
        if (i==0) {
            [self onTabButtonClik:btn];
        }
    }
    [self.tabBar setClipsToBounds:YES];
}
-(void)onTabButtonClik:(id)sender{
    
    UIButton *btn=(UIButton*)sender;
    //未被選中的按鈕復位
    for (UIView *v in self.tabBar.subviews) {
        if ([v isKindOfClass:[UIButton class]]&&v.tag>0&&v.tag<4&&v.tag!=btn.tag) {
            UIButton *btnv=(UIButton *)v;
            [btnv setSelected:NO];
        }
    }
    //select the selected tab
    [btn setSelected:YES];
    [self setSelectedIndex:btn.tag-1];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved