UINavigationController详解与使用(二)页面切换和segmentedController

本文深入解析UINavigationController在iOS开发中的重要作用,包括如何实现页面间的跳转、自定义返回按钮及添加segmentedController。通过实例演示,详细讲解了RootViewController到SecondView的跳转过程,以及如何在不同场景下自定义返回按钮和segmentedController的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UINavigationController详解与使用(一)添加UIBarButtonItem是上篇,我们接着讲UINavigationController的重要作用,页面的管理和切换。


运行效果图:
[img]
[img]http://dl.iteye.com/upload/attachment/0078/5540/3c585209-2dcc-3d6f-a960-89c31e31deff.jpg[/img]
[/img]

[img]
[img]http://dl.iteye.com/upload/attachment/0078/5544/fdaa80ba-fb89-3197-831b-2ed062c2bd8d.jpg[/img]
[/img]

1、RootView 跳到SecondView

首先我们需要新一个View。新建SecondView,按住Command键然后按N,弹出新建页面,我们新建SecondView

2、为Button 添加点击事件,实现跳转
在RootViewController.xib中和RootViewController.h文件建立连接
在RootViewController.m中实现代码,alloc一个SecondViewController,用pushViewController到navigationController中去,并为

SecondViewController这是title为 secondView.title =@"Second View"; 默认情况下,titie为下个页面返回按钮的名字。
- (IBAction)gotoSecondView:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondView animated:YES];
secondView.title = @"Second View";
}


3.1、添加segmentedController
在RootViewController.m的viewDidLoad添加如下代码:
 NSArray *array = [NSArray arrayWithObjects:@"鸡翅",@"排骨", nil];
UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];
segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;

[segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentedController;


3.2[segmentedController addTarget:selfaction:的实现
-(void)segmentAction:(id)sender
{
switch ([sender selectedSegmentIndex]) {
case 0:
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了鸡翅" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];

}
break;
case 1:
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了排骨" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
}
break;

default:
break;
}
}


4、自定义backBarButtonItem

左上角的返回上级View的barButtonitem的名字是上级目录的Title,如果title或者适合做button的名字,怎么办呢?我们可以自己定义
代码如下:
 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根视图" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarBu

我是新手,主意添加位置:RootViewControll.m中
- (void)viewDidLoad
{
[super viewDidLoad];
//add leftButton 添加左按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];
self.navigationItem.leftBarButtonItem = leftButton;

// add rightButton 添加右按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(selectRightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;


// add backButton 添加返回按钮
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:@"Root View" style:UIBarButtonItemStyleDone target:self action:nil];
self.navigationItem.backBarButtonItem = backButton;

//add SegmentedControl on Navigation 添加分段
NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two" ,@"three",nil];
UISegmentedControl *segmentdeController = [[UISegmentedControl alloc] initWithItems:array];
segmentdeController.segmentedControlStyle = UISegmentedControlSegmentCenter;
[segmentdeController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentdeController;




}


5、自定义title
UINavigationController的title可以用别view替代,比如用UIButton UILable等,下面我用UIButton.
在SecondViewController.m中添加下面如下:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button setTitle: @"自定义title" forState: UIControlStateNormal];
[button sizeToFit];
self.navigationItem.titleView = button;}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值