您的位置:首页 > 移动开发 > Objective-C

Objective--C 导航视图控制器

2015-12-24 19:48 513 查看
导航视图器相当于视图器的一个总管理者,在根视图创建后,任何一个视图控制器都能使用

一:创建

// 创建视图控制器

RootViewController *rootVC = [[RootViewController alloc] init];

// 创建导航视图控制器

UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];

二:修改导航视图器

// 修改导航视图控制器的时候一般从外观和内容上进行修改

1 . 修改外观

// 修改导航视图控制器的半透明效果,默认是YES

self.navigationController.navigationBar.translucent = NO;

// 修改背景颜色

self.navigationController.navigationBar.barTintColor =
[UIColor yellowColor];

2.修改内容

// (1).标题

self.title = @"天猫首页";

// 改变标题

self.navigationItem.title = @"豆瓣电影";

// 可以把label放到titleView上,能改变label的内容,比如字体大小等

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

label.text = @"淘宝";

label.textAlignment = NSTextAlignmentCenter;

self.navigationItem.titleView = label;

label.font = [UIFont systemFontOfSize:25];

[label release];

// 不设置title(类似于QQ聊天界面的消息通话界面)

UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"消息",@"通话"]];

self.navigationItem.titleView =
seg;

[seg release];

// (2)创建按钮

// 创建左边的按钮

self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(barAction:)];

// 第二种方式(创建右边按钮,图片)

// self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"money.png"] style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)] autorelease];

// 第三种方式(创建右边确认按钮)

// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];

// 第四种,使用自定义视图(常用)

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"money.png"] forState:UIControlStateNormal];

button.frame = CGRectMake(0, 0, 40, 40);

[button addTarget:self action:@selector(barAction:) forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithCustomView:button];

UIButton *pushBuootn = [UIButton buttonWithType:UIButtonTypeSystem];

pushBuootn.frame = CGRectMake(100, 100, 150, 50);

pushBuootn.layer.borderWidth = 1;

pushBuootn.layer.cornerRadius = 5;

[pushBuootn setTitle:@"下一页" forState:UIControlStateNormal];

[self.view addSubview:pushBuootn];

[pushBuootn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

//跳转到下一页的Button点击事件

- (void)click:(UIButton *)button{

// 方式一:模态跳转

// 创建跳转到得页面

// SecondViewController *secVC = [[SecondViewController alloc] init];

// [secVC setModalTransitionStyle:0];

// [self presentViewController:secVC animated:YES completion:^{

//

// }];

// 方式二:通过导航视图控制器进行跳转操作

// 1.创建下一页的目标对象

SecondViewController *secVC = [[SecondViewController alloc] init];

// 2.跳转

[self.navigationController pushViewController:secVC animated:YES];

// 3.内存管理

[secVC release];

}

// 返回首页

[self.navigationController popToRootViewControllerAnimated:YES];

// 返回上一页

[self.navigationController popViewControllerAnimated:YES];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: