AppDelegate.m里面需要实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *VC = [ViewController new];
self.navtionViewControl = [[UINavigationController alloc]initWithRootViewController:VC];
self.navtionViewControl.navigationBar.hidden = YES;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window setRootViewController:self.navtionViewControl];
[self.window makeKeyAndVisible];
return YES;
}
自定义导航栏.h文件里面需要实现
#import <UIKit/UIKit.h>
@protocol publicNavtionDelegate <NSObject>
@required
- (void)rightBtnSelect;
@optional
- (void)leftBtnSelect;
@end
@interface PublicNavtionBar : UIView
@property (nonatomic,assign) id<publicNavtionDelegate>navtioonBarDelegate;
//通用的初始化方法
-(id)initWithLeftBtn:(BOOL)leftBtnControl
withTitle:(NSString *)strTitle
withRightBtn:(BOOL)rightBtnControl
withRightBtnPicName:(NSString *)picName
withRightBtnSize:(CGSize)picSize;
@end
自定义导航栏.m文件里面需要实现
#import "PublicNavtionBar.h"
#import "AppDelegate.h"
#define MainView_Width [UIScreen mainScreen].bounds.size.width
#define IOS7_Y ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0?20:0)
#define kWide [UIScreen mainScreen].bounds.size.width
#define kPercenX kWide / 320
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define color_pink RGBCOLOR(223, 79, 140)
@implementation PublicNavtionBar
@synthesize navtioonBarDelegate = _navtionBarDelegate;
- (id)initWithLeftBtn:(BOOL)leftBtnControl withTitle:(NSString *)strTitle withRightBtn:(BOOL)rightBtnControl withRightBtnPicName:(NSString *)picName withRightBtnSize:(CGSize)picSize{
self = [super initWithFrame:CGRectMake(0, 0, MainView_Width, 44 + IOS7_Y)];
if (self)
{
//设置导航条背景颜色
[self setBackgroundColor:color_pink];
if (leftBtnControl) {
//返回按钮
UIButton * btnBack = [[UIButton alloc]init];
[btnBack setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[btnBack setBackgroundImage:[UIImage imageNamed:@"Public_btnBack.png"] forState:UIControlStateNormal];
[btnBack setFrame:CGRectMake(kPercenX, IOS7_Y, 44, 44)];
[btnBack addTarget:self action:@selector(btnBack) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnBack];
}
UILabel * labelTitle = [[UILabel alloc]init];
[labelTitle setText:strTitle];
[labelTitle setFrame:CGRectMake(0, IOS7_Y, MainView_Width, 44)];
[labelTitle setTextColor:[UIColor blackColor]];
[labelTitle setBackgroundColor:[UIColor clearColor]];
[labelTitle setTextAlignment:NSTextAlignmentCenter];
[labelTitle setFont:[UIFont boldSystemFontOfSize:18]];
[self addSubview:labelTitle];
//右侧按钮
if (rightBtnControl) {
UIButton * btnRight = [[UIButton alloc]init];
[btnRight setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnRight setBackgroundImage:[UIImage imageNamed:picName] forState:UIControlStateNormal];
[btnRight setFrame:CGRectMake(MainView_Width-picSize.width-10, IOS7_Y+(44-picSize.height)/2, picSize.width,picSize.height)];
[btnRight addTarget:self action:@selector(rightBtnSelelct) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btnRight];
}
}
return self;
}
//右侧按钮点击方法
-(void)rightBtnSelelct{
[self.navtioonBarDelegate rightBtnSelect];
}
-(void)btnBack{
if([self.navtioonBarDelegate respondsToSelector:@selector(leftBtnSelect)]){
[self.navtioonBarDelegate leftBtnSelect];
}
else
{
AppDelegate * appdelegate = [UIApplication sharedApplication].delegate;
[appdelegate.navtionViewControl popViewControllerAnimated:YES];
}
}
@end
然后再创建一个单例(创建单例的方法)
.H文件里面需要实现
#import <Foundation/Foundation.h>
@interface Animal : NSObject
+(instancetype)shareTimeH;
@property (nonatomic,copy)NSString *name;
@end
.m里面需要实现的
#import "Animal.h"
@implementation Animal
+ (instancetype)shareTimeH{
static Animal *anima;
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
anima = [[[self class]alloc]init];;
});
return anima;
}
seconderViewController.h 里面需要实现
#import "seconderViewController.h"
#import "PublicNavtionBar.h"
#import "ViewController.h"
#import "Animal.h"
@interface seconderViewController ()
{
UITextField *textField;
}
@end
@implementation seconderViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.navigationController.navigationBar.hidden = YES;
PublicNavtionBar *bar = [[PublicNavtionBar alloc]initWithLeftBtn:YES withTitle:@"返回" withRightBtn:YES withRightBtnPicName:@"column_drag_normal.png" withRightBtnSize:CGSizeMake(44, 44)];
[self.view addSubview:bar];
textField = [UITextField new];
textField.backgroundColor = [UIColor grayColor];
textField.frame = CGRectMake(100, 100, 200, 50);
[self.view addSubview:textField];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
Animal *anima = [Animal shareTimeH];
anima.name = textField.text;
[self.navigationController popViewControllerAnimated:YES];
}
ViewController.h 里面需要实现
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic,strong)UILabel *lable;
@end
ViewController.m 里面需要实现
#import "ViewController.h"
#import "PublicNavtionBar.h"
#import "seconderViewController.h"
#import "Animal.h"
@interface ViewController ()<publicNavtionDelegate>
{
Animal *anima;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
PublicNavtionBar *navBar = [[PublicNavtionBar alloc]initWithLeftBtn:NO withTitle:@"王维传" withRightBtn:YES withRightBtnPicName:@"column_drag_normal.png" withRightBtnSize:CGSizeMake(40, 40)];
[navBar setNavtioonBarDelegate:self];
[self.view addSubview:navBar];
anima = [Animal shareTimeH];
_lable = [UILabel new];
_lable.backgroundColor = [UIColor redColor];
_lable.frame = CGRectMake(100, 100, 200, 50);
[self.view addSubview:_lable];
}
- (void)viewWillAppear:(BOOL)animated{
_lable.text = anima.name;
NSLog(@"--->%@",anima.name);
}
- (void)rightBtnSelect{
seconderViewController *secondVC = [seconderViewController new];
[self.navigationController pushViewController:secondVC animated:YES];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
seconderViewController *secondVC = [seconderViewController new];
[self.navigationController pushViewController:secondVC animated:YES];
}