UISB 导航控制器基础

scenedelegate.m

#import "SceneDelegate.h"
#import "VCRoot.h"
@interface SceneDelegate ()

@end

@implementation SceneDelegate


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
    self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    
    //创建一个根视图控制器
    VCRoot* root=[[VCRoot alloc]init];
    //创建导航控制器
    //导航控制器主要用来管理多个视图控制器切换
    //层级的方式管理多个视图控制器
    //创建控制器的时候,一定要有根控制器
    //p1 作为导航控制器的根控制器
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:root];
    //将window的根视图设置为导航控制器
    self.window.rootViewController=nav;
    [self.window makeKeyAndVisible];
    
    
    
}

VCroot.m

#import "VCRoot.h"

@interface VCRoot ()

@end

@implementation VCRoot

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor yellowColor];
    //设置导航栏的标题文字
    self.title=@"根视图";
    
    //若navigationItem.title=@"title"; nil self.title为标题
    self.navigationItem.title=@"title";
    
    //创建一个左侧按钮
    //P1 按钮文字
    //P2 按钮风格
    //P3 事件拥有着
    //P4 按钮事件
    UIBarButtonItem* leftbtn =[[UIBarButtonItem alloc]initWithTitle:@"左侧" style:UIBarButtonItemStyleDone target:self action:@selector(pressleft)];
    
    //将导航元素赋值
    self.navigationItem.leftBarButtonItem=leftbtn;
    //创建右侧按钮
    //根据系统风格创建按钮
    //只需要指定风格样式 系统风格的按钮内容或标题文字不能改变
    //P1 按钮风格
    //p2 事件拥有着
    //p3 按钮事件
    
    
    UIBarButtonItem* rightbtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pressRight)];
    
//    self.navigationItem.rightBarButtonItem=rightbtn;
    
    
    UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 40)];
    label.text=@"text";
    
    
    label.textColor=[UIColor blueColor];
    
    //将任何类型的控件添加到导航按钮的方法
    
    UIBarButtonItem* item03=[[UIBarButtonItem alloc] initWithCustomView:label];
    //创建按钮数组
    NSArray* arraybtn = [NSArray arrayWithObjects:rightbtn,item03, nil];
    //将右侧按钮赋值
    
    self.navigationItem.rightBarButtonItems=arraybtn;
    
    
    
    
}

-(void)pressleft
{
    NSLog(@"点击左侧");
    
}


-(void)pressRight
{
    NSLog(@"点击右侧");
}

@end
原文地址:https://www.cnblogs.com/zhangqing979797/p/13736987.html