项目:片刻 DAY1 搭建框架结构
来源:互联网 发布:阿里云盒子刷安卓系统 编辑:程序博客网 时间:2024/06/04 18:05
关于片刻,它的根视图控制器有点非主流,并不是通常所见到的UITabbarController,然后在UITabbarController的子视图上添加带有UINavigationController的各种常规控制器(一般的有UIViewController,UITableViewController),而它的根视图并不是通常我们所说的容器控制器。而是一个UIViewController,在UIViewController上面有UIVIew,UITableView,还有播放器等视图。而通过点击UITableView上面的Cell可以跳转到不同的视图控制器。它的架构同现在所使用的QQ有点像,这里我们称它为抽屉控制器。
下面是两款软件中的“抽屉控制器”
其实在写这个项目之前,我也试着写过如何实现这种抽屉效果的代码;首先你要明白的是,第一次进入的界面并不是根视图控制器所直接对应的主界面。
废话不多说,直接上代码。
根视图控制器的那个界面
// .h文件#import <UIKit/UIKit.h>@interface PKRootLeftViewController : UIViewController@end// .m文件#import "PKRootLeftViewController.h"#import "PKRootRightViewController.h" // 导入显示界面的控制器(并非根视图控制器)@interface PKRootLeftViewController ()<UITableViewDataSource,UITableViewDelegate> // 下面UITableVIew必须要接受协议和数据源@property (nonatomic, strong) PKRootRightViewController *rootRightVC;@property (nonatomic, strong) UITableView *mainListView;// 主列表@property (nonatomic, strong) NSArray *dataSourceArray;// 提供数据源@end@implementation PKRootLeftViewController- (void)dealloc{// ARC和MRC下都不允许手动调用,但是可以重写,ARC重写的时候不可以出现[super dealloc]和release,一般用于移除观察者,代理对象置空等操作 NSLog(@"%@被释放了",self);}- (NSArray *)dataSourceArray{ if (_dataSourceArray == nil) { self.dataSourceArray = @[@"电台",@"阅读",@"社区",@"良品"]; } return _dataSourceArray;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.dataSourceArray count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; cell.textLabel.text = self.dataSourceArray[indexPath.row]; return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self.rootRightVC changeViewWithIndex:indexPath.row];}- (void)viewDidLoad { [super viewDidLoad]; self.mainListView = [[UITableView alloc]initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH - 40, SCREEN_HEIGHT - 280) style:(UITableViewStylePlain)]; self.mainListView.dataSource = self; self.mainListView.delegate = self; self.mainListView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];// 处理多余的空格线 [self.view addSubview:self.mainListView]; [self.mainListView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];// 添加右侧主页视图控制器(这里必须要将rootRightVC定义成属性,不然执行完[self.view addSubview:_rootRightVC.view];时rootRightVC就被释放掉了,你对_rootRightVC.view进行操作时很有可能崩溃,比如点击按钮的操作,target当时调用的self指的是_rootRightVC,而此时已经被释放,必然崩) _rootRightVC = [[PKRootRightViewController alloc]init];// ARC里面,编译器(LLVM)会自动帮我们添加release代码,添加的时机就是这个对象出作用域的时候,所以我们的工程被编译成二进制文件的时候是有release代码的,只是我们看不见而已 [self.view addSubview:_rootRightVC.view];}
进入应用第一个显示的界面
.h文件@interface PKRootRightViewController : UIViewController- (void)changeViewWithIndex:(NSInteger)index; // 用于与Cell进行关联- (void)changeFrame;.m文件#import "PKRootRightViewController.h"#import "PKBaseViewController.h"@interface PKRootRightViewController ()@property(nonatomic, strong)NSArray *classArray;@property(nonatomic, strong)PKBaseViewController *baseVC;@end@implementation PKRootRightViewController- (PKBaseViewController *)baseVC{ if (_baseVC == nil) { self.baseVC = [[PKBaseViewController alloc]init]; } return _baseVC;}- (NSArray *)classArray // 类名的数组,用来下面直接创建对应的对象{ if (_classArray == nil) { self.classArray = @[@"PKRadioViewController",@"PKReadMainViewController",@"PKGroupMainViewController",@"PKProductMainViewController"]; } return _classArray;}- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor orangeColor]; self.baseVC = [[NSClassFromString(self.classArray[0]) alloc]init]; self.baseVC.changeFrameVC = self; [self.view addSubview:self.baseVC.view];}- (void)changeFrame{// CGRect newFrame = self.view.frame; if (newFrame.origin.x == 0) { newFrame.origin.x = SCREEN_WIDTH - 40; }else { newFrame.origin.x = 0; } [UIView animateWithDuration:0.2 animations:^{ self.view.frame = newFrame; }];}- (void)changeViewWithIndex:(NSInteger)index{ /* switch (index) { case 0: { // 创建电台控制器,把电台控制器的View添加到自己的视图上面 } break; case 1: { // 创建阅读控制器,把阅读控制器的View添加到自己的视图上面 } break; case 2: { // 创建社区控制器,把社区控制器的View添加到自己的视图上面 } break; case 3: { // 创建良品控制器,把良品控制器的View添加到自己的视图上面 } break; default: break; }*/ // 用NSClassFromString函数通过字符串获取到某个类,然后动态的创建不同的控制器,同时使用多态的思想,让父类的指针接受子类的对象。 [self.baseVC.view removeFromSuperview];// 将上一次的view给删除掉 self.baseVC = [[NSClassFromString(self.classArray[index]) alloc]init]; self.baseVC.changeFrameVC = self; [self.view addSubview:self.baseVC.view]; [self changeFrame];}@end
由于下面几个类@”PKRadioViewController”,@”PKReadMainViewController”,@”PKGroupMainViewController”,@”PKProductMainViewController”有共同的特征,所以创建了一个基类PKBaseViewController
.h 文件#import <UIKit/UIKit.h>#import "PKRootRightViewController.h"@interface PKBaseViewController : UIViewController{ @protected UIView *_topView; UILabel *_titleLabel;}@property (nonatomic, weak) PKRootRightViewController *changeFrameVC;@property (nonatomic, strong) UILabel *titleLabel;@property (nonatomic, strong) UIView *topView;@end.m文件#import "PKBaseViewController.h"@interface PKBaseViewController ()@property (nonatomic, strong) UIButton *leftBtn;@end@implementation PKBaseViewController- (UIView *)topView{ if (!_topView) { self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 50)]; [_topView addSubview:self.titleLabel]; [self.view addSubview:_topView]; // 创建左侧按钮 _leftBtn = [UIButton buttonWithType:(UIButtonTypeSystem)]; _leftBtn.frame = CGRectMake(5, 30, 30, 30); [_leftBtn setTitle:@"三" forState:(UIControlStateNormal)]; [_leftBtn addTarget:self action:@selector(changViewFrame:) forControlEvents:(UIControlEventTouchUpInside)]; [self.view addSubview:_leftBtn]; } return _topView;}- (UILabel *)titleLabel{ if (!_titleLabel) { self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 100, 30)]; } return _titleLabel;}- (void)changViewFrame:(UIButton *)button{ // 改变位置 [self.changeFrameVC changeFrame];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"%@",self.topView);//这行代码最为关键,因为上面采用懒加载,所以必须要调用topView的GET方法,它才会创建!!!!! self.topView.backgroundColor = [UIColor whiteColor];}
0 0
- 项目:片刻 DAY1 搭建框架结构
- 商务综合管理-day1-项目搭建
- 片刻
- 关于spring项目框架结构修复
- 小项目练手-Day1
- MyBatis入门初级教程(2) – 框架结构搭建
- android项目框架结构实现、封装、继承
- vue项目框架结构入门 及 vue基础
- 淘淘商城项目:Day1+Day2
- 片刻随想
- 片刻休息
- 发愣片刻
- 休息片刻
- 片刻雨
- Android day1:Java开发环境搭建
- 关于实施MVC框架结构的WEB项目的一些见解
- 关于实施MVC框架结构的WEB项目的一些见解
- 关于实施MVC框架结构的WEB项目的一些见解
- 指针和引用的区别
- 错误的结果2......VC/bin/cl.exe返回的最终解决方案
- 你真的需要一个小例子来了解一些事情!
- UITableView默认选中第一个cell
- Python 安装matplotlib,six,dateutil,pyparsing 完整过程
- 项目:片刻 DAY1 搭建框架结构
- iOS改变状态栏的字体颜色
- 当我开始爱自己
- Github入门
- Android开发学习解决程序运行就出错的一个问题
- Uncaught SyntaxError: Failed to execute 'querySelectorAll' on 'Element': '#00' is not a valid select
- 如何使用MySQL处理数据——增删改查
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- 一幅图弄清DFT与DTFT,DFS的关系