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

来源:互联网 发布:数据质量盘点标准 编辑:程序博客网 时间:2024/05/22 16:02

AppDelegate.m里面需要实现


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    ViewController *VC = [ViewControllernew];

    self.navtionViewControl = [[UINavigationControlleralloc]initWithRootViewController:VC];

    self.navtionViewControl.navigationBar.hidden = YES;

    

    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.windowsetRootViewController:self.navtionViewControl];

    [self.windowmakeKeyAndVisible];

    returnYES;

}


自定义导航栏.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 = [superinitWithFrame:CGRectMake(0,0,MainView_Width,44 +IOS7_Y)];

    if (self)

    {

        //设置导航条背景颜色

        [selfsetBackgroundColor:color_pink];

    if (leftBtnControl) {

        //返回按钮

        UIButton * btnBack = [[UIButtonalloc]init];

        [btnBack setTitleColor:[UIColorgreenColor] forState:UIControlStateNormal];

        [btnBack setBackgroundImage:[UIImageimageNamed:@"Public_btnBack.png"]forState:UIControlStateNormal];

        [btnBack setFrame:CGRectMake(kPercenX,IOS7_Y, 44,44)];

        [btnBack addTarget:selfaction:@selector(btnBack)forControlEvents:UIControlEventTouchUpInside];

        [selfaddSubview:btnBack];

    }

    UILabel * labelTitle = [[UILabelalloc]init];

    [labelTitle setText:strTitle];

    [labelTitle setFrame:CGRectMake(0,IOS7_Y, MainView_Width,44)];

    [labelTitle setTextColor:[UIColorblackColor]];

    [labelTitle setBackgroundColor:[UIColorclearColor]];

    [labelTitle setTextAlignment:NSTextAlignmentCenter];

    [labelTitle setFont:[UIFontboldSystemFontOfSize:18]];

    [selfaddSubview:labelTitle];

    //右侧按钮

    if (rightBtnControl) {

        UIButton * btnRight = [[UIButtonalloc]init];

        [btnRight setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];

        [btnRight setBackgroundImage:[UIImageimageNamed:picName] forState:UIControlStateNormal];

        [btnRight setFrame:CGRectMake(MainView_Width-picSize.width-10,IOS7_Y+(44-picSize.height)/2, picSize.width,picSize.height)];

        [btnRight addTarget:selfaction:@selector(rightBtnSelelct)forControlEvents:UIControlEventTouchUpInside];

        [selfaddSubview:btnRight];

    }

}

    returnself;

}


//右侧按钮点击方法

-(void)rightBtnSelelct{

    [self.navtioonBarDelegaterightBtnSelect];

}

-(void)btnBack{

    if([self.navtioonBarDelegaterespondsToSelector:@selector(leftBtnSelect)]){

        [self.navtioonBarDelegateleftBtnSelect];       

    }

    else

    {

        AppDelegate * appdelegate = [UIApplicationsharedApplication].delegate;

        

        [appdelegate.navtionViewControlpopViewControllerAnimated: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{

    staticAnimal *anima;

    staticdispatch_once_t onceToken;

    dispatch_once(&onceToken,^{

        anima = [[[selfclass]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 {

    [superviewDidLoad];    

    self.view.backgroundColor = [UIColorredColor];

    self.navigationController.navigationBar.hidden = YES;

    PublicNavtionBar *bar = [[PublicNavtionBaralloc]initWithLeftBtn:YESwithTitle:@"返回"withRightBtn:YESwithRightBtnPicName:@"column_drag_normal.png"withRightBtnSize:CGSizeMake(44,44)];

    [self.viewaddSubview:bar];

    

    textField = [UITextFieldnew];

    textField.backgroundColor = [UIColorgrayColor];

    textField.frame =CGRectMake(100,100, 200,50);

    [self.viewaddSubview:textField];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{


    Animal *anima = [AnimalshareTimeH];

    anima.name =textField.text;

    [self.navigationControllerpopViewControllerAnimated: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 {

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColorgreenColor];

    PublicNavtionBar *navBar = [[PublicNavtionBaralloc]initWithLeftBtn:NOwithTitle:@"王维传"withRightBtn:YESwithRightBtnPicName:@"column_drag_normal.png"withRightBtnSize:CGSizeMake(40,40)];

    [navBar setNavtioonBarDelegate:self];

    [self.viewaddSubview:navBar];

    

    anima = [AnimalshareTimeH];

    _lable = [UILabelnew];

    _lable.backgroundColor = [UIColorredColor];

    _lable.frame =CGRectMake(100,100, 200,50);

    

    [self.viewaddSubview:_lable];

}


- (void)viewWillAppear:(BOOL)animated{


    _lable.text =anima.name;

    NSLog(@"--->%@",anima.name);

}



- (void)rightBtnSelect{


    seconderViewController *secondVC = [seconderViewControllernew];

    [self.navigationControllerpushViewController:secondVCanimated:YES];

}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{


    seconderViewController *secondVC = [seconderViewControllernew];

    [self.navigationControllerpushViewController:secondVCanimated:YES];

}


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














1 0
原创粉丝点击