UI12-导航栏视图控制器的使用

新建工程,创建三个类继承自UIViewController,分别是FirstViewController、SecondViewController 、ThirdViewController

一、编写APPDelegate.m文件中代码:

#import "AppDelegate.h"

#import "FirstViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate


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

    

    [selfsetWindow:[[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds]];

    [self.windowmakeKeyAndVisible];

    

    FirstViewController *firstController = [[FirstViewControlleralloc]init];

    

    //导航栏控制器,需要指定一个根视图控制器

    //导航栏控制器主要用来管理子视图。

    UINavigationController *navController = [[UINavigationControlleralloc]initWithRootViewController:firstController];

    //给控制器设置背景颜色,一般不需要这么做

    [navController.viewsetBackgroundColor:[UIColorwhiteColor]];

    [self.windowsetRootViewController:navController];

    returnYES;

}

@end

二、编写FirstViewController.m中代码:

#import "FirstViewController.h"

#import "SecondViewController.h"


@interface FirstViewController ()


@end


@implementation FirstViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    [selfsetTitle:@"First"];

    [self.viewsetBackgroundColor:[UIColorgrayColor]];

    //给导航栏添加按钮,这里选用系统自带的UIBarButtonSystemItemAdd类型的按钮

    UIBarButtonItem *item1 = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:NULL];

    //添加一个自定义的按钮样式

    UIButton *itemButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    //起始坐标被限制,所以一般就写为(00

    [itemButton setFrame:CGRectMake(0,0,80,30)];

    [itemButton setBackgroundColor:[UIColorlightGrayColor]];

    [itemButton setTitle:@"Item"forState:UIControlStateNormal];

    [itemButton setTitle:@""forState:UIControlStateHighlighted];

    UIBarButtonItem *item2 = [[UIBarButtonItemalloc]initWithCustomView:itemButton];

//

//    self.navigationItem.leftBarButtonItem = item1;

    //添加多个按钮,传入数组类型

    self.navigationItem.leftBarButtonItems =@[item1, item2];

    //给导航栏设置背景颜色,默认有一层毛玻璃特效

    self.navigationController.navigationBar.backgroundColor = [UIColorblueColor];

    //给导航栏设置背景图片

    /*渲染样式

    UIBarMetricsDefault:原样显示图片

    UIBarMetricsCompact:如果已经设置颜色将显示设置的颜色,如果没有设置就用当前子视图的颜色渲染

     

     */

    [self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"bg_navBar"]forBarMetrics:UIBarMetricsDefault];

    //创建另一个Button

    //当导航栏有背景图片后,按钮将frame坐标从导航栏下方开始计算

    //当只是设置背景颜色后,按钮是从左上角开始计算坐标

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [button setFrame:CGRectMake(100,80,100,30)];

    [button setBackgroundColor:[UIColorblackColor]];

    [button setTitle:@"Push"forState:UIControlStateNormal];

    [button setTitle:@""forState:UIControlStateHighlighted];

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

    //给按钮绑定点击事件

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

    [self.viewaddSubview:button];

}


//不同视图控制器之间的跳转

-(void)pushController

{

    //跳转到下一个视图控制器动画效果animated:传入YES or NO

    [self.navigationControllerpushViewController:[[SecondViewControlleralloc]init]animated:YES];

    

}


@end


三、编写SecondViewController.m中文件

#import "SecondViewController.h"

#import "ThirdViewController.h"


@interface SecondViewController ()


@end


@implementation SecondViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfsetTitle:@"Second"];

    [self.viewsetBackgroundColor:[UIColoryellowColor]];

    //设置导航栏标题的颜色

    [self.navigationController.navigationBarsetTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColorwhiteColor]}];

    

    UIBarButtonItem *leftItem = [[UIBarButtonItemalloc]initWithTitle:@"POP"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(popController)];

    self.navigationItem.leftBarButtonItem = leftItem;

    UIBarButtonItem *rightItem = [[UIBarButtonItemalloc]initWithTitle:@"Push"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(pushController)];

     self.navigationItem.rightBarButtonItem = rightItem;


}


-(void)popController

{

    [self.navigationControllerpopViewControllerAnimated:YES];

}


-(void)pushController

{

    [self.navigationControllerpushViewController:[[ThirdViewControlleralloc]init]animated:YES];

}


@end


四、编写ThirdViewController.m中文件

#import "ThirdViewController.h"


@interface ThirdViewController ()


@end


@implementation ThirdViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfsetTitle:@"Third"];

    [self.viewsetBackgroundColor:[UIColorwhiteColor]];

    

    UIBarButtonItem *leftItem = [[UIBarButtonItemalloc]initWithTitle:@"POP"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(popController)];

    self.navigationItem.leftBarButtonItem = leftItem;

}


-(void)popController

{

    //返回到上一个界面

//    [self.navigationController popViewControllerAnimated:YES];

    //返回到主视图界面

    [self.navigationControllerpopToRootViewControllerAnimated:YES];

   //获取子视图控制器

//    NSLog(@"%@", self.navigationController.viewControllers);

    //跳转到指定子视图控制器对象

//    [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];

}


@end


运行效果:
      


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值