开源框架的使用介绍
关于VTMagic的集成方法主要有以下两种:
1. 直接实例化VTMagicController对象,然后添加到当前控制器中。
- (void)viewDidLoad { [super viewDidLoad]; [self addChildViewController:self.magicController]; [self.view addSubview:_magicController.view]; [_magicController didMoveToParentViewController:self]; [_magicController.magicView reloadData]; } - (VTMagicController *)magicController { if (!_magicController) { _magicController = [[VTMagicController alloc] init]; _magicController.magicView.navigationColor = [UIColor whiteColor]; _magicController.magicView.sliderColor = [UIColor redColor]; _magicController.magicView.layoutStyle = VTLayoutStyleDivide; _magicController.magicView.switchStyle = VTSwitchStyleDefault; _magicController.magicView.navigationHeight = 40.f; _magicController.magicView.dataSource = self; _magicController.magicView.delegate = self; } return _magicController; }
2. 继承VTMagicController,然后在viewDidLoad中完成相应配置。
#import “VTMagicController.h” @interface ViewController : VTMagicController @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.magicView.navigationColor = [UIColor whiteColor]; self.magicView.sliderColor = [UIColor redColor]; self.magicView.layoutStyle = VTLayoutStyleDefault; self.magicView.switchStyle = VTSwitchStyleDefault; self.magicView.navigationHeight = 40.f; self.magicView.dataSource = self; self.magicView.delegate = self; [self.magicView reloadData]; }
VTMagicViewDataSource协议
不管是通过以上哪种方法集成的,都需要实现数据源协议《 VTMagicViewDataSource 》,主要有以下三个方法:
其它
重要协议
除了数据源协议《 VTMagicViewDataSource 》外 ,VTMagic中的重要协议还有《 VTMagicViewDelegate 》和《 VTMagicReuseProtocol 》。前者用于在主控制器中处理页面切换事件;后者用于子控制器被重用时,清除旧数据等逻辑处理。
VTMagicViewDelegate协议
- (void)magicView:(VTMagicView *)magicView viewDidAppeare:(UIViewController *)viewController atPage:(NSUInteger)pageIndex { NSLog(@“pageIndex:%ld viewDidAppeare:%@”,pageIndex, viewController.view); } - (void)magicView:(VTMagicView *)magicView viewDidDisappeare:(UIViewController *)viewController atPage:(NSUInteger)pageIndex { NSLog(@“pageIndex:%ld viewDidDisappeare:%@”,pageIndex, viewController.view); } - (void)magicView:(VTMagicView *)magicView didSelectItemAtIndex:(NSUInteger)itemIndex { NSLog(@“didSelectItemAtIndex:%ld”, (long)itemIndex); }
VTMagicReuseProtocol
- (void)vtm_prepareForReuse { NSLog(@“clear old data if needed:%@”, self); }
其它
你可以在任意子控制器中,通过self.magicController获取最近的上层主控制器,magicController遵循协议《 VTMagicProtocol 》,以便完成一些必要的跨层级的逻辑处理,前提是你需要import文件。
NSInteger currentPage = self.magicController.currentPage; UIViewController *viewController = self.magicController.currentViewController;
切换到指定页面,页面切换有两种方式:
[self.magicView switchToPage:3 animated:YES];
或者
[self.magicController switchToPage:3 animated:YES];
获取指定页面控制器,同样有两种方式:
UIViewController *viewController = [self.magicView viewControllerAtPage:3];
或者
UIViewController *viewController = [self.magicController viewControllerAtPage:3];
非常好我支持^.^
(0) 0%
不好我反对
(0) 0%