iOS控件之UINavigationView(导航控制器)

iOS控件之导航控制器

这段代码在AppDelegate.m中

#import "AppDelegate.h"
#import "VCRoot.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  {
	self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
	//创建一个根视图控制器
	VCRoot* root = [[VCRoot alloc] init];
	//创建导航控制器
	//导航控制器主要用来管理多个视图控制器的切换
	//层级的方法来管理多个视图控制器
	//创建导航控制器时,一定要有一个根视图控制器
	//参数一:就是作为导航控制器的根视图控制器
	UINavigationController* nav = [[UINavigationController alloc] initWithRooyViewController:root];

	//将window的根视图控制器设置为导航
	self.window.rootViewController = nav;

	[self.window makeKeyAndVisible];
	return YES;
}

这段代码写在VCRoot.h中
- (view)viewDidLoad {
	self.view.backgroundColor = [UIColor yellowColor];
	//设置导航控制器的标题文字
	self.title = @"根视图";

	//设置导航元素项目的标题
	//如果没有设置navigationItem.title,为nil;
	//系统会使用self.title作为标题
	//如果navigationItem.title不为空
	//将navigationItem.title设置为标题内容
	self.navigationItem.title = @"Title";

	//创建一个导航栏左侧的按钮
	//根据tiitle文字来创建按钮
	//p1:按钮上的文字
	//p2:按钮风格
	//p3:事件拥有者
	//p4:按钮事件
	UIBarButtonItem* leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"左侧" style:UIBarNuttonItemStyleDone target:self action:@selector(pressLeft)];

	//将导航元素项的左侧按钮赋值
	self.navigationItem.leftBarButtonItem = leftBtn;

	//创建一个导航栏右侧的按钮
    //根据系统风格来创建按钮
    //只需要指定风格样式,系统风格的按钮内容或标题文字不能改变
    //P1:按钮风格
    //P2:事件拥有者
    //P3:按钮事件
    UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(pressRight)];
    
    self.navigationItem.rightBarButtonItem = rightBtn;

	//标签对象
	UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 40)];
	label.text = @"test";
	label.textAlignment = NSTextAlignmentCenter;
	label.textColor = [UIColor blueColor];
	
	//将任何类型的控件添加到导航栏按钮的方法
	UIBarButtonItem* item03 = [[UIBarButtonItem alloc] initWithCustomView:label];

	//创建按钮数组
	NSArray* arrayBtn = [NSArray arrayWithObjects:rightBtn, item03, nil];
	//将右侧按钮数组赋值
	self.navigationItem.rightBarButtonItems = arrayBtn;
}

- (void)pressRight {
	NSLog(@"右侧按钮被按下!");
}
- (void) pressLeft {
	NSlog(@"左侧按钮被按下!");
}

###介绍一点高级的

这段代码在VCRoot.m中
//设置导航栏的透明度
//默认透明度为YES:可透明的
//NO:使导航栏不透明
self.navigationController.navigationBar.translucent = NO;
设置导航栏风格
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
self.view.backgroundColor = [UIColor blueColor];
self.title = @"根视图";
UIBarButtonItem *next = [[UIBarbuttonItem alloc] initWithTitle:@"下一级" style:UIBarButtonItemStylePlain target:self action:@selector(pressNext)];
self.navigationItem.rightBarButtonItem = next;

-(void) pressNext {
    //创建新的视图控制器
    VCSecond* vcSecond = [[VCSecond alloc] init];
    
    //使用当前视图控制器的导航控制器对象
    [self.navigationController pushViewController:vcSecond animated:YES];
    
}

这段代码在VCSecond.m中
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    
    self.title = @"视图二";
    
    UIBarButtonItem* btnNext = [[UIBarButtonItem alloc] initWithTitle:@"第三级" style:UIBarButtonItemStylePlain target:self action:@selector(pressNext)];
    
    self.navigationItem.rightBarButtonItem = btnNext;
}

-(void) pressNext {
    VCThird* vc = [[VCThird alloc] init];
    //推入第三个视图控制器
    [self.navigationController pushViewController:vc animated:YES];
}
这段代码在VCThird.m中
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor orangeColor];
    self.title = @"第三级";
    UIBarButtonItem* btnLeft = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(pressBack)];
    
    //当自己设定左侧按钮时
    //返回按钮会被左侧按钮替换
    self.navigationItem.leftBarButtonItem = btnLeft;
    
    UIBarButtonItem* btnRight = [[UIBarButtonItem alloc] initWithTitle:@"返回根视图" style:UIBarButtonItemStylePlain target:self action:@selector(pressRight)];
    
    self.navigationItem.rightBarButtonItem = btnRight;
}

-(void) pressRight {
    //直接返回到根视图
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void) pressBack {
    //将当前视图控制器弹出,返回上一级界面
    [self.navigationController popViewControllerAnimated:YES];
}

注意:导航栏和工具栏是公用的,但是它们上面的按钮是独立的。它们是属于视图控制器的

你可能感兴趣的:(iOS)