点击TabBar 跳转怎么实现跳转,我用了一个静态变量,问下更好的办法?

点击TabBar 跳转怎么实现跳转(UITabBarController 的 子控制器的 选择),我用了一个静态变量 mainTabBarCtrl ,
暴露出 - (void)setTabBarCornerMarkCount 方法 ,实现 外部Ctrl 调用选择指定的 UITabBarController 的 子控制器 展示。
在 - (void)setTabBarCornerMarkCount 方法 ,调用UITabBarController 的 - (void)setSelectedIndex: (NSUInteger)selectedIndex 方法 。
代码如下:
问下更好的办法?

.h 文件, MainTabBarViewController

@interface MainTabBarViewController : UITabBarController

- (void)chooseButtonCurrentSelected:(NSInteger ) btnIndex;
- (void)setTabBarCornerMarkCount;

@end

@interface UIViewController(MainTabBarViewControllerSupport)
 @property (nonatomic, readonly) MainTabBarViewController * mainTabBarCtrl; @end

.m 文件 MainTabBarViewController

static MainTabBarViewController * mainTabBarCtrl;


@implementation UIViewController (MainTabBarViewControllerSupport)

- (MainTabBarViewController *)mainTabBarCtrl {
    return mainTabBarCtrl; }

@end


@interface MainTabBarViewController () @property (nonatomic, strong) NSArray * childItems; @property (nonatomic, strong) NSMutableArray * itemButtons; @property (nonatomic, strong) TabBarButton * buttonCurrentSelected; @end

@implementation MainTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    mainTabBarCtrl = self;
    [self animateZheLaunch ];
    [self initTabBar ]; }

- (void) initTabBar {
    self.childItems = @[
                        @{kClass: @"FrontPageViewController",
                          kTitle: @"首页",
                          kImage: @"home_off",
                          kSelectedImage: @"home_on"
                          },
                        @{kClass: @"ShoppingViewController",
                          kTitle: @"逛街",
                          kImage: @"shopping_off",
                          kSelectedImage: @"shopping_on"
                          },
                        @{kClass: @"CartViewController",
                          kTitle: @"购物车",
                          kImage: @"shopping_bag_off",
                          kSelectedImage: @"shopping_bag_on"
                          },
                        @{kClass: @"MineViewController",
                          kTitle: @"我的",
                          kImage: @"mine_off",
                          kSelectedImage: @"mine_on"
                          }
                        ];

    self.itemButtons = [NSMutableArray array ];
    NSMutableArray * tempArray = [[NSMutableArray alloc ] init ];
    for (NSDictionary * dict in self.childItems) {
        UIViewController * viewController = [[NSClassFromString(dict[kClass]) alloc ] init ];
        BasePremierNaviViewController * navigationViewController = [[ BasePremierNaviViewController alloc ] initWithRootViewController: viewController ];
        [tempArray addObject: navigationViewController ];
    }
   
    self.viewControllers = tempArray ;
    UIView * tabBarContainerView = [[UIView alloc ] initWithFrame: CGRectMake(0, 0, kScreenWidth, 50) ];
    tabBarContainerView.backgroundColor = [UIColor whiteColor ];
    tabBarContainerView.userInteractionEnabled = YES;
    [self.tabBar addSubview: tabBarContainerView ];
    
    [self.tabBar setBackgroundImage: [UIImage createImageWithColor: [CHUtil colorWithHexString: @"#DDDDDD" ] withSize: CGSizeMake(kScreenWidth, 1.f)] ];
    [self.tabBar setShadowImage: [UIImage createImageWithColor: [CHUtil colorWithHexString: @"#DDDDDD" ] withSize: CGSizeMake(kScreenWidth, 1.f)] ];
    CGFloat itemButtonWidth = kScreenWidth/[self.childItems count];
    for (int i = 0; i < [self.childItems count]; i++){
        NSDictionary * tempDict = [self.childItems objectAtIndex: i ];
        TabBarButton * itemButton = [self createTabBarItemButtonWithFrame: CGRectMake(i * itemButtonWidth, 0, itemButtonWidth, 50) withTitle: tempDict[kTitle] withNormalStateImage: [UIImage imageNamed: tempDict[kImage] ] withSelectedStateImage: [UIImage imageNamed: tempDict[kSelectedImage] ] withTargrt: self withAction: @selector( selectTabBarItem:)];
        switch (i) {
            case 0:{
                itemButton.centerXButton = kScreenWidth/8+5;
                }
                break;
            case 3:{
                itemButton.centerXButton = kScreenWidth/8-5;
                }
                break;
            default:
                {
                itemButton.centerXButton = kScreenWidth/8;
                }
                break;
        }
        itemButton.tag = 20 + i;
        [tabBarContainerView addSubview: itemButton ];
        if (i == 0){
            itemButton.selected = YES;
            self.buttonCurrentSelected = itemButton;
        }
        [self.itemButtons addObject: itemButton ];
    } }


- (void) selectTabBarItem: (TabBarButton *) button {
    if (button != self.buttonCurrentSelected){
        button.selected = YES;
        self.buttonCurrentSelected.selected = NO;
        self.buttonCurrentSelected = button;
    }
    self.selectedIndex = button.tag - 20; }


#pragma mark - Set

- (void)chooseButtonCurrentSelected:(NSInteger ) btnIndex {

    TabBarButton * barBtn = [self.itemButtons objectAtIndex: btnIndex ];
    barBtn.selected = YES;
    self.buttonCurrentSelected.selected = NO;
    self.buttonCurrentSelected = barBtn;
    self.selectedIndex = btnIndex; }

// 重写 setSelectedIndex
- (void)setSelectedIndex: (NSUInteger)selectedIndex {
    if (self.childItems.count < selectedIndex){
        return;
    }
    [super setSelectedIndex: selectedIndex ];

}
阅读 3.3k
1 个回答

iOS程序犭袁 的 openSource,
CYLTabBarController

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题