iOS 三大特性 继承 多态 以及 封装
下面就是对继承的更为深刻的认识。
假如你准备重写NavigationView 那么就需要让其自动生成返回键 以及其他功能。
如:
#import@interface ParentClassController : UIViewController@property(nonatomic,strong)UIView * NavigationView;//头部导航栏@property(nonatomic,strong)UIButton * NavigationBack;//返回上级菜单@property(nonatomic,strong)UILabel * TitleName;//标题@property(nonatomic,strong)UIButton * Share;//分享-(void)initVariable;//初始化变量-(void)initView;//初始化界面-(void)initData;//初始化数据-(void)setNavigationItems;//设置导航栏信息
#import "ParentClassController.h"@interface ParentClassController ()@end@implementation ParentClassController- (void)viewDidLoad { [super viewDidLoad]; [self initVariable]; [self initData]; [self initView]; [self setNavigationItems];}#pragma mark--初始化变量-(void)initVariable{ }#pragma mark--初始化界面-(void)initView{ /* 跳转模式:原生4种 1. 默认:UIModalTransitionStyleCoverVertical 上下弹出 2.UIModalTransitionStyleFlipHorizontal 翻转 3.UIModalTransitionStyleCrossDissolve 淡入 4.UIModalTransitionStylePartialCurl 翻页动画 */}#pragma mark--初始化数据-(void)initData{ }#pragma mark--设置导航栏信息-(void)setNavigationItems{ //基类最基础的返回 主题 以及去购物车 //导航 _NavigationView=[[UIView alloc]initWithFrame:CM(0, 20, VIEW_WIDTH, 44)]; _NavigationView.backgroundColor=UINavigationColor; _NavigationView.layer.borderWidth=0.4; _NavigationView.layer.borderColor=[UIColor grayColor].CGColor; [self.view addSubview:_NavigationView]; //返回 _NavigationBack=[UIButton buttonWithType:UIButtonTypeCustom]; _NavigationBack.tag=1; _NavigationBack.frame=CM(10, 10, 25, 25); UIImage *imgMenu =[[UIImage imageNamed:@"Back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [_NavigationBack setImage:imgMenu forState:UIControlStateNormal]; [_NavigationBack setImage:[UIImage imageNamed:@"back-1" ]forState:UIControlStateHighlighted]; [_NavigationBack addTarget:self action:@selector(HomeNavigationButton:) forControlEvents:UIControlEventTouchUpInside]; [self.NavigationView addSubview:_NavigationBack]; //主题 _TitleName=[[UILabel alloc]initWithFrame:CM(VIEW_WIDTH/2-50, 12, 100, 20)]; _TitleName.text=@"海涛展示厅"; _TitleName.textColor=[UIColor whiteColor]; _TitleName.textAlignment = NSTextAlignmentCenter; [self.NavigationView addSubview:_TitleName]; //分享 _Share=[[UIButton alloc]initWithFrame:CM(VIEW_WIDTH-40, 10, 25, 25)]; _Share.tag=2; UIImage * imageShare=[[UIImage imageNamed:@"分享" ]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [_Share setImage:imageShare forState:UIControlStateNormal]; [_Share setImage:[UIImage imageNamed:@"分享-1"] forState:UIControlStateHighlighted]; [self.NavigationView addSubview:_Share]; [_Share addTarget:self action:@selector(HomeNavigationButton:) forControlEvents:UIControlEventTouchUpInside]; self.view.backgroundColor=[UIColor whiteColor]; }#pragma mark--界面按钮-(void)HomeNavigationButton:(UIButton *)sender{ switch (sender.tag) { case 0: { } break; case 1: { // 返回上一级 [self dismissViewControllerAnimated:YES completion:nil]; // 返回首页[self.navigationController popToRootViewControllerAnimated:YES]; NSLog(@"返回"); } case 2: { NSLog(@"分享"); } default: break; }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}
下面这个类继承了
#import//注意继承@interface SearchViewController : ParentClassController@end
//// Created by 杭州晶泰电子商务有限公司 on 16/3/16.// Copyright © 2016年 杭州晶泰电子商务有限公司. All rights reserved.//#import "SearchViewController.h"@interface SearchViewController ()@end@implementation SearchViewController- (void)viewDidLoad { [super viewDidLoad]; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
这个类就是就是一个所有的Controller的基类
只要继承该类就会自动生成返回键以及分享功能
注意:在继承的时候,如果遇到调用两次方法 那么可能你在子类中调用了该方法。并且你在
子类中的viewDidLoad中调用了[super viewDidLoad];
那么子类重写了父类的方法那么你调用[super viewDidLoad]
在子类中就不要在调用重写的方法了,因为[super viewDidLoad]已经调用过了。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}