iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
2013-06-02 13:10
381 查看
1、RootView 跳到SecondView
首先我们需要新一个View。新建SecondView,按住Command键然后按N,弹出新建页面,我们新建SecondView
![](http://my.csdn.net/uploads/201206/21/1340248827_2596.png)
2、为Button 添加点击事件,实现跳转
在RootViewController.xib中和RootViewController.h文件建立连接
![](http://my.csdn.net/uploads/201206/21/1340248977_7550.png)
在RootViewController.m中实现代码,alloc一个SecondViewController,用pushViewController到navigationController中去,并为
SecondViewController这是title为 secondView.title =@"Second View"; 默认情况下,titie为下个页面返回按钮的名字。
[cpp] view
plaincopy
- (IBAction)gotoSecondView:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondView animated:YES];
secondView.title = @"Second View";
}
这是点击GotoSecondView 按钮,出现
![](http://my.csdn.net/uploads/201206/21/1340249372_7450.png)
这就是SecondView了。
3、添加segmentedController
在nav bar这样的效果是如何实现的呢?
![](http://my.csdn.net/uploads/201206/21/1340249618_5572.png)
这就是segmentedController。
3.1在RootViewController.m的viewDidLoad添加如下代码:
[cpp] view
plaincopy
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:的实现
[cpp] view
plaincopy
-(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的名字,怎么办呢?我们可以自己定义
代码如下:
[cpp] view
plaincopy
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根视图" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarBu
效果:
![](http://my.csdn.net/uploads/201206/21/1340253865_6351.png)
6、自定义title
UINavigationController的title可以用别view替代,比如用UIButton UILable等,下面我用UIButton.
在SecondViewController.m中添加下面如下。
[cpp] view
plaincopy
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button setTitle: @"自定义title" forState: UIControlStateNormal];
[button sizeToFit];
self.navigationItem.titleView = button;}
运行程序,goto secondView,运行效果
![](http://my.csdn.net/uploads/201206/21/1340254352_1746.png)
例子代码:https://github.com/schelling/YcDemo
著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢
首先我们需要新一个View。新建SecondView,按住Command键然后按N,弹出新建页面,我们新建SecondView
![](http://my.csdn.net/uploads/201206/21/1340248827_2596.png)
2、为Button 添加点击事件,实现跳转
在RootViewController.xib中和RootViewController.h文件建立连接
![](http://my.csdn.net/uploads/201206/21/1340248977_7550.png)
在RootViewController.m中实现代码,alloc一个SecondViewController,用pushViewController到navigationController中去,并为
SecondViewController这是title为 secondView.title =@"Second View"; 默认情况下,titie为下个页面返回按钮的名字。
[cpp] view
plaincopy
- (IBAction)gotoSecondView:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondView animated:YES];
secondView.title = @"Second View";
}
这是点击GotoSecondView 按钮,出现
![](http://my.csdn.net/uploads/201206/21/1340249372_7450.png)
这就是SecondView了。
3、添加segmentedController
在nav bar这样的效果是如何实现的呢?
![](http://my.csdn.net/uploads/201206/21/1340249618_5572.png)
这就是segmentedController。
3.1在RootViewController.m的viewDidLoad添加如下代码:
[cpp] view
plaincopy
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:的实现
[cpp] view
plaincopy
-(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的名字,怎么办呢?我们可以自己定义
代码如下:
[cpp] view
plaincopy
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根视图" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarBu
效果:
![](http://my.csdn.net/uploads/201206/21/1340253865_6351.png)
6、自定义title
UINavigationController的title可以用别view替代,比如用UIButton UILable等,下面我用UIButton.
在SecondViewController.m中添加下面如下。
[cpp] view
plaincopy
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button setTitle: @"自定义title" forState: UIControlStateNormal];
[button sizeToFit];
self.navigationItem.titleView = button;}
运行程序,goto secondView,运行效果
![](http://my.csdn.net/uploads/201206/21/1340254352_1746.png)
例子代码:https://github.com/schelling/YcDemo
著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢
相关文章推荐
- iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
- 使用selenium+python;在页面A点击一个按钮跳转到页面B,页面A没有关闭,浏览器打开了一个B,切换窗口定位页面B的元素
- Swift - 使用导航条和导航条控制器来进行页面切换
- IOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
- 使用 vue-router 切换页面时怎么设置过渡动画
- Android使用ActionBar和ViewPager切换页面
- ViewPager的使用方法(实现左右滑动切换页面)
- 【转】C# 系统应用之使用Pancel控件同一窗体切换页面
- Android使用ViewFlipper做页面切换,与手势滑动切换的使用
- TabNavigator中页面切换使用Move效果
- 使用ViewPager进行页面滑动切换
- ViewPager的使用(有底部小图片可切换页面)
- 使用ViewFlipper实现页面自动切换
- iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
- iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
- Android使用Fragment打造万能页面切换框架(二)
- IOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
- Android使用Fragment打造万能页面切换框架
- 安卓app页面跟随手指滑动而切换 ViewPager的使用 ViewPager+Fragment
- iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController