通过控制switch来删除增加tabBar

.h @property (nonatomic, retain)UISwitch *switch1;

.m 

self.switch1 = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 200, 100)];

[self.switch1 addTarget:self action:@selector(switchClicked:) forControlEvents:UIControlEventValueChanged];

// 通过控制switch 推回主视图层

UITabBarController *tab = (UITabBarController *)[[[[UIApplication shareApplication] delegate] window] rootViewController];

// 把不同的tabBarControllers 放入数组中

NSMutableArray *array = (NSMutableArray *)[tab viewControllers];

if (5 == array.count) {

//  开关是关的(默认)

    [self.switch1 isOn];   

} else {

    self.switch1.on = YES;

}  

[self.scroll addSubview:self.switch1];

[self.switch1 release];



- (void)switchClicked:(UISwitch *)switch1

   if ([self.switch1 isOn]) {

       UITabBarController *tab = (UITabBarController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController];

       NSMutableArray *array = (NSMutableArray *)[tab viewControllers];

       [array removeObjectAtIndex:2];

        tab.viewControllers = array;

   } else {

//  给推向层 tabBar 初始化 起名图片

    WCMainViewController *wc = [[WCMainViewController alloc] init];

    wc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"世界杯" image:[UIImage iamgeNamed:@"worldcup.png"] tag:2];


    UITabBarController *tab = (UITabBarController *)[[[[UIApplication shareApplication] delegate] window] rootViewController];

    NSMutableArray *array = (NSMutableArray *)[tab viewControllers];

    [array insertObject:wc atIndex:2];

    tab.viewControllers = array;

   }

}


你可能感兴趣的:(通过控制switch来删除增加tabBar)