自定义导航栏(附带利用单例传值)

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];

}


这就是一个很简单的自定导航栏和简单的利用单例模式传值,各位看到如果有什么不妥的地方希望各位大牛多多指教。














  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值