继承是面向对象编程语言的三大特性之一,写好基类会给App的开发带来极大的方便。在iOS开发中,一般一个页面就对应一个ViewController,ViewController在开发中用的也很多,写一个好的ViewController的基类,会让开发变得轻松很多。

可以写一个BaseViewController,继承自UIViewController,在这里可以实现一些整个项目里ViewController的公共方法,最常用的就是导航栏相关的。一般App在设计的时候,导航栏的风格都比较同意,导航栏相关的方法可以放在基类里面实现,然后让其他的功能ViewController继承BaseViewController,这样导航栏部分就不用每个界面都处理了。

下面是我自己写的一个BaseViewController,具体的实现也要看App的主要功能,基类一般实现共性的功能,代码仅供参考,有更好建议的也欢迎评论留言交流。

BaseViewController.h代码:

#import <UIKit/UIKit.h>

@interface BZBaseViewController : UIViewController
//返回按钮
@property(nonatomic,strong) UIButton * backButton;
//导航栏标题
@property(nonatomic,strong) UILabel * navigationTitleLabel;
//导航栏右按钮(图片)
@property(nonatomic,strong) UIButton * rightButton;
//导航栏右按钮(文字)
@property(nonatomic,strong) UIButton * rightTextButton; //为了灵活的满足不同的ViewController,将set方法放到.h文件,供子类调用
-(void)setupNavigationItem;
-(void)setBackButton;
-(void)setRightButton;
-(void)setNavigationTitleLabel;
-(void)setRightTextButton; //返回按钮和右按钮点击方法,如果需要实现不同的方法,子类可以重新该方法
-(void)navBackClick;
-(void)navRightClick;
-(void)navRightTextClick; @end

BaseViewController.m代码:

#import "BZBaseViewController.h"

@interface BZBaseViewController ()

@end

@implementation BZBaseViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//ViewController的背景颜色,如果整个App页面背景颜色比较统一,建议在这里设置
self.view.backgroundColor = COLOR_COMMON_LIGHTGRAY;
//设置导航栏
[self setupNavigationItem];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.hidesBackButton = YES;
if (iOS11) {
//scrollerView在导航栏透明时不下压
[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}else{
self.automaticallyAdjustsScrollViewInsets = NO;
} } -(void)setupNavigationItem{
//导航栏背景
UIImage * image = [[UIImage imageNamed:@"img_navigationbar_bg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(-1, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
} -(void)setBackButton{
//设置返回按钮
UIBarButtonItem * backBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
self.navigationItem.leftBarButtonItem = backBarButton;
} -(void)setRightButton{
//设置右按钮(图片)
UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
self.navigationItem.rightBarButtonItem = rightBarButton;
} -(void)setRightTextButton{
//设置右按钮(文字)
UIBarButtonItem * rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.rightTextButton];
self.navigationItem.rightBarButtonItems = @[[self getNavigationSpacerWithSpacer:0],rightBarButton];
} -(void)setNavigationTitleLabel{
//设置标题
self.navigationItem.titleView = self.navigationTitleLabel;
} -(UIBarButtonItem *)getNavigationSpacerWithSpacer:(CGFloat)spacer{
//设置导航栏左右按钮的偏移距离
UIBarButtonItem *navgationButtonSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
navgationButtonSpacer.width = spacer;
return navgationButtonSpacer;
} #pragma mark - lazy 各控件的初始化方法
-(UIButton *)backButton{
if (!_backButton) {
_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
_backButton.frame = CGRectMake(0, 0, 50, 40);
[_backButton setImage:[UIImage imageNamed:@"button_nav_back"] forState:UIControlStateNormal];
_backButton.titleLabel.font = [UIFont systemFontOfSize:17];
[_backButton setContentEdgeInsets:UIEdgeInsetsMake(0, -40, 0, 0)];
[_backButton addTarget:self action:@selector(navBackClick) forControlEvents:UIControlEventTouchUpInside];
}
return _backButton;
} -(UIButton *)rightButton{
if (!_rightButton) {
_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightButton.frame = CGRectMake(0, 0, 40, 40);
[_rightButton addTarget:self action:@selector(navRightClick) forControlEvents:UIControlEventTouchUpInside];
}
return _rightButton;
} -(UIButton *)rightTextButton{
if (!_rightTextButton) {
_rightTextButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightTextButton.frame = CGRectMake(0, 0, 60, 40);
_rightTextButton.titleLabel.font = [UIFont systemFontOfSize:17];
[_rightTextButton addTarget:self action:@selector(navRightTextClick) forControlEvents:UIControlEventTouchUpInside];
}
return _rightTextButton;
} -(UILabel *)navigationTitleLabel{
if (!_navigationTitleLabel) {
_navigationTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 150, 30)];
_navigationTitleLabel.font = [UIFont systemFontOfSize:17];
_navigationTitleLabel.textColor = [UIColor whiteColor];
_navigationTitleLabel.textAlignment = NSTextAlignmentCenter;
}
return _navigationTitleLabel;
} #pragma mark - click 导航栏按钮点击方法,右按钮点击方法都需要子类来实现
-(void)navBackClick{
[self.navigationController popViewControllerAnimated:YES];
} -(void)navRightClick{ } -(void)navRightTextClick{ }

写好基类之后,让其他页面继承于该基类,导航栏相关的设置直接调用这里面的方法即可,子类主要用来写自己的功能性代码就好,用起来也是很方便的。

当然如果要开发的App每个页面的导航栏差异比较大,UI风格也相差比较大的话可能就不太实用了,合适的才是最好的,需不需要基类、需要什么样的基类还是看具体项目。希望能给开发的小伙伴们带来帮助。

【iOS】UIViewController基类的实现的更多相关文章

  1. 无线客户端框架设计(3):基类的设计(iOS篇)

    本文代码:YoungHeart-Chapter-03.zip 没有基类的App都不是好App. 因为iOS使用的是mvc模式的开发模式,所以,业务逻辑基本都在每个页面相应的ViewController ...

  2. iOS Foundation 框架基类

    iOS Foundation 框架基类 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转 ...

  3. iOS 基于MVC设计模式的基类设计

    iOS 基于MVC设计模式的基类设计 https://www.jianshu.com/p/3b580ffdae00

  4. iOS控制器之基类设计

    题记 在进入新公司后.经过这一个月的重构项目,终于把项目做到了个人相对满意的程度(还有一种不满意的叫老板的需求,提过多次意见也没用= =!).在这次重构中按照以前的思路设计出了个人觉得比较适用的一个基 ...

  5. 【iOS】 含tableView的ViewController基类的实现

    上篇博客写了ViewController的基类的实现,这篇博客主要写在BaseViewController的基础上实现一个含tableView控件的基类的实现,主要给包含tableView的页面来继承 ...

  6. ios中解析json对象基类

    这个是对上面一篇写的一个解析json对象的基类 @interface BaseObjectFromJson : NSObject + (id) objectWithDict:(NSDictionary ...

  7. iOS多线程常用类说明--备用参考

    iOS的多线程,涉及到如下一些类,这里集中做个介绍,免得混淆. 1.NSTimer 很显然,这是定时器类 2.NSTask iOS 不支持 NSTask 在很多并发操作的时候,多线程太耗资源,也太危险 ...

  8. 基类中定义的虚函数在派生类中重新定义时,其函数原型,包括返回类型、函数名、参数个数、参数类型及参数的先后顺序,都必须与基类中的原型完全相同 but------> 可以返回派生类对象的引用或指针

      您查询的关键词是:c++primer习题15.25 以下是该网页在北京时间 2016年07月15日 02:57:08 的快照: 如果打开速度慢,可以尝试快速版:如果想更新或删除快照,可以投诉快照. ...

  9. IOS上传图片方法类

    IOS上传图片方法类   iPhone开发中遇到上传图片问题,找到多资料,最终封装了一个类,请大家指点,代码如下 // // RequestPostUploadHelper.h // demodes ...

随机推荐

  1. Spring Mobile——探测客户端设备和系统

    Spring Mobile--探测客户端设备和系统 今天闲来无事,浏览Spring的官方网站,发现了Spring Mobile项目,之前也看到过,还以为是针对手机端的项目,并没有细看.今天仔细看了一下 ...

  2. 5分钟学习spark streaming之 轻松在浏览器运行和修改Word Counts

    方案一:根据官方实例,下载预编译好的版本,执行以下步骤: nc -lk 9999 作为实时数据源 ./bin/run-example org.apache.spark.examples.sql.str ...

  3. 分享PHP中的10个实用函数

    分享PHP中的10个实用函数 PHP的功能越来越强大,里面有着非常丰富的内置函数.资深的PHP程序员对它们可能都很熟悉,但很多参加PHP培训的PHP初学者,仍然对一些非常有用的函数不太熟悉.这篇文章里 ...

  4. TypeScript入门(三)面向对象特性

    一.类(Class) 类是ts的核心,使用ts开发时,大部分代码都是写在类里面. 1.类的声明 多个对象有相同的属性和方法,但是状态不同. 声明类的属性和方法时可以加 访问控制符,作用是:类的属性和方 ...

  5. centos7下更改docker镜像和容器的默认路径

    笔者近期在服务器上搭建docker环境,可由于笔者是普通用户,在安装的时候就跳了很多坑,现在记录一下. 一.docker权限问题 据官方解释,搭建docker环境必须使用root权限,或者sudo装, ...

  6. PAT-L2-007-gplt真题

    题目分析: 1. 首先,题目说一个家庭有孩子爸爸妈妈等几辈人,可以利用并查集将一个家庭里的所有人变成一个集合: 2. 刚好题目的目的也是这样,输出的是一个家庭人数,人均房产面积,人均房产套数等: 3. ...

  7. day1-计算机基础

    第一单元  计算机组成原理 一.概念及过程 1.进行逻辑和数值高速计算的计算机器,有存储功能,能按照程序自动执行,且能够处理海量数据的现代化电子设备. 2.发展过程 数学运算:算盘,帕斯卡的齿轮装置, ...

  8. Nginx+Tomcat 配置负载均衡集群

    一.Hello world 1.前期环境准备 准备两个解压版tomcat,如何同时启动两个tomcat,请看我的另一篇文章<一台机器同时启动多个tomcat>. nginx官网下载解压版n ...

  9. Redis分布式锁---完美实现

    这几天在做项目缓存时候,因为是分布式的所以需要加锁,就用到了Redis锁,正好从网上发现两篇非常棒的文章,来和大家分享一下. 第一篇是简单完美的实现,第二篇是用到的Redisson. Redis分布式 ...

  10. EF Core利用Transaction对数据进行回滚保护

    What? 首先,说一下什么是EF Core中的Transaction Transaction允许以原子方式处理多个数据库操作,如果事务已提交,则所有操作都应用于数据库,如果事务回滚,则没有任何操作应 ...