通过控制switch来删除增加tabBar

来源:互联网 发布:医师 软件 编辑:程序博客网 时间:2024/06/10 02:52

.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;

   }

}


0 0
原创粉丝点击