UINavigationController配合Segment

http://www.apkbus.com/android-131344-1-2.html
运行效果图:
[img] 
 
[/img] 

[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为下个页面返回按钮的名字。
  1. - (IBAction)gotoSecondView:(id)sender {
  2.     SecondViewController *secondView = [[SecondViewController alloc] init];
  3.     [self.navigationController pushViewController:secondView animated:YES];
  4.     secondView.title = @"Second View";
  5. }
复制代码
3.1、添加segmentedController 
     在RootViewController.m的viewDidLoad添加如下代码:
  1. NSArray *array = [NSArray arrayWithObjects:@"鸡翅",@"排骨", nil];
  2.     UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];
  3.     segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;

  4.     [segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
  5.     self.navigationItem.titleView = segmentedController;
复制代码
3.2[segmentedController addTarget:selfaction:的实现
  1. -(void)segmentAction:(id)sender
  2. {
  3.     switch ([sender selectedSegmentIndex]) {
  4.         case 0:
  5.         {
  6.             UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了鸡翅" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  7.             [alter show];

  8.         }
  9.         break;
  10.     case 1:
  11.         {
  12.             UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了排骨" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  13.             [alter show];
  14.         }
  15.         break;
  16.         
  17.         default:
  18.             break;
  19.     }
  20. }
复制代码
4、自定义backBarButtonItem 

左上角的返回上级View的barButtonitem的名字是上级目录的Title,如果title或者适合做button的名字,怎么办呢?我们可以自己定义 
代码如下:
  1. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根视图" style:UIBarButtonItemStyleDone target:nil action:nil];
  2.     self.navigationItem.backBarBu
复制代码
我是新手,主意添加位置:RootViewControll.m中 
  
  1. - (void)viewDidLoad
  2. {
  3.     [super viewDidLoad];
  4.     //add leftButton 添加左按钮
  5.     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];
  6.     self.navigationItem.leftBarButtonItem = leftButton;
  7.     
  8.     // add rightButton 添加右按钮
  9.     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd  target:self action:@selector(selectRightAction:)];
  10.     self.navigationItem.rightBarButtonItem = rightButton;
  11.     
  12.     
  13.     // add backButton 添加返回按钮
  14.     UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:@"Root View" style:UIBarButtonItemStyleDone target:self action:nil];
  15.     self.navigationItem.backBarButtonItem = backButton;
  16.     
  17.     //add SegmentedControl on Navigation  添加分段
  18.     NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two" ,@"three",nil];
  19.     UISegmentedControl *segmentdeController = [[UISegmentedControl alloc] initWithItems:array];
  20.     segmentdeController.segmentedControlStyle = UISegmentedControlSegmentCenter;
  21.     [segmentdeController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
  22.     self.navigationItem.titleView = segmentdeController;
  23.     
  24.     
  25.     
  26.     
  27. }
复制代码
5、自定义title 
UINavigationController的title可以用别view替代,比如用UIButton UILable等,下面我用UIButton. 
在SecondViewController.m中添加下面如下:
  1. - (void)viewDidLoad
  2. {
  3.     [super viewDidLoad];
  4.     UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  5.     [button setTitle: @"自定义title" forState: UIControlStateNormal];
  6.     [button sizeToFit];
  7.     self.navigationItem.titleView = button;}
复制代码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值