您的位置:首页 > 移动开发 > IOS开发

IOS学习(十七)多视图管理

2016-12-15 00:05 127 查看
#import "MainViewController.h"
#import "CustomTabBar.h"
#import "OCViewController.h"
#import "JAVAViewController.h"
#import "PHPViewController.h"

@interface MainViewController ()<CustomTabBarDelegate>

@property(nonatomic, strong) NSArray *titles;
@property(nonatomic, strong) NSMutableArray *arrayViews;
@property(nonatomic, strong) NSMutableArray *arrayViewControllers;
@property(nonatomic, strong) CustomTabBar *tabBar;
@end

@implementation MainViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];

self.titles = @[@"OCView", @"JAVAView", @"PHPView"];

//    [self addViews:self.titles];
[self addViewControllers];

self.tabBar = [CustomTabBar tabBarWithTites:self.titles];

[self.tabBar setDelegate:self];
[self.view addSubview:self.tabBar];

//    [self tabBar:tabBar itemSelectedAtIndex:0];
//    [self.tabBar itemButtonSelected:0];

}

- (NSMutableArray *)arrayViewControllers{
if (!_arrayViewControllers){
_arrayViewControllers = [NSMutableArray array];
}

return _arrayViewControllers;
}

- (NSMutableArray *)arrayViews{

if (!_arrayViews){
_arrayViews = [NSMutableArray array];
}

return _arrayViews;
}

- (void)addViewControllers{
OCViewController *oc = [[OCViewController alloc] init];
[self.arrayViewControllers addObject:oc];
[self.view addSubview:oc.view];
//必须
[self addChildViewController:oc];

JAVAViewController *java = [[JAVAViewController alloc] init];
[self.arrayViewControllers addObject:java];
[self.view addSubview:java.view];
[self addChildViewController:java];

PHPViewController *php = [[PHPViewController alloc] init];
[self.arrayViewControllers addObject:php];
[self.view addSubview:php.view];
[self addChildViewController:php];
}

- (void)addViews:(NSArray *)titles{
for (NSString *className in titles) {
UIView *view = [[NSClassFromString(className) alloc] init];

[self.view addSubview:view];
[self.arrayViews addObject:view];
}

}

- (void)tabBar:(CustomTabBar *)tabBar itemSelectedAtIndex:(NSInteger)index{
//    UIView *view = self.arrayViews[index];
//
//    [self.view bringSubviewToFront:view];
//    [self.view bringSubviewToFront:tabBar];

UIViewController *controller = self.arrayViewControllers[index];
[self.view bringSubviewToFront:controller.view];
[self.view bringSubviewToFront:tabBar];
}
//内存紧张时
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];

NSArray *tmep = [self.arrayViewControllers copy];
for (NSInteger i = 0; i < tmep.count ; i ++){
if (i == self.currentIndex){
continue;
}

UIViewController * ct = tmep[i];
[ct removeFromParentViewController];
[ct.view removeFromSuperview];
[self.arrayViewControllers removeObject:ct];
ct = nil;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: