Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 250|回复: 0

[默认分类] iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-6-4 10:10:30 | 显示全部楼层 |阅读模式


    iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem是上篇,我们接着讲UINavigationController的重要作用,页面的管理和切换。
    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. }
    复制代码
    这是点击GotoSecondView 按钮,出现


    这就是SecondView了。


    3、添加segmentedController
    在nav bar这样的效果是如何实现的呢?


    这就是segmentedController。

    3.1在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
    复制代码
    效果:


    6、自定义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;}
    复制代码
    运行程序,goto secondView,运行效果




    下篇文件讲下Navigation 的Toobar如何显示和如何自己定义。
    下篇:
    iOS学习之UINavigationController详解与使用(三)ToolBar






      著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢


      










    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-24 11:15 , Processed in 0.299269 second(s), 35 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表