View跳转到UITabBarViewController(一)
来源:互联网 发布:部落冲突闪电升级数据 编辑:程序博客网 时间:2024/06/05 08:37
当在AppDelegate里使用Tabbar,这样的程序 打开就是TabbarView了,有时候 我们需要给程序做一些帮助页面,或者登录页面,之后 才跳转到tabbar View里,或者后面的页面才使用Tabbar的,那这样怎么实现 呢?
我们建立一个视图,然后在这个视图通过[self presentModalViewController:tabBaranimated:YES];跳转来实现 。
当程序中需要在多个View直接切换的时候,可以使用UINavigationController,也可以用ModalViewController。UINabigationController是通过导航条来切换多个View。而如果View的数量比较少,且显示领域为全屏的时候,用ModalViewController就比较合适(比如需要用户输入信息的View,结束后自动回复到之前的View)
1.新建一个Single View app,按Command+N新建三个ViewController,都选上.xib文件。
1.1 新建的Controller分别是:TestOneController TestTwoController TestThirdViewController ,他们都继承UIViewController。
单击xib文件,在xib文件的属性窗口里修改View的颜色,这样好在切换页面的时候区分出来是切换了页面。
好吧,我的ThirdViewController没有xib,可能是漏了,不过也没关系,一样能用
1.2 添加TabBarViewController
最重要的是再添加一个TabBarViewController,
这个需要继承的UITabBarController
注意:这个地方一定要小心,我就犯错了,继承了UIViewController,同志们,注意了,啦 啦 啦 啦!
2、在ViewController也就是程序进来的第一个页面。在这里添加一个跳转的Button,并加上Action,然后在Action里实现跳转到tabbar.
ViewController.m里实现代码。这就跳转,把刚才建立的三个ViewController都添加到Tabbar里
- (IBAction)gotoTabbarVIew:(id)sender {
NSMutableArray *items = [[NSMutableArray alloc] init];
TestOneController *testOne1 = [[TestOneController alloc] init];
[items addObject:testOne1];
TestTwoController *twoController = [[TestTwoController alloc] init];
[items addObject:twoController];
TestThirdViewController *thirdController = [[TestThirdViewController alloc] init];
[items addObject:thirdController];
// items是数组,每个成员都是UIViewController
TabBarViewController *tabBar = [[TabBarViewController alloc] init];
[tabBar setTitle:@"TabBarController"];
[tabBar setViewControllers:items];
[self presentModalViewController : tabBar animated:YES];
}
NSMutableArray *items = [[NSMutableArray alloc] init];
TestOneController *testOne1 = [[TestOneController alloc] init];
[items addObject:testOne1];
TestTwoController *twoController = [[TestTwoController alloc] init];
[items addObject:twoController];
TestThirdViewController *thirdController = [[TestThirdViewController alloc] init];
[items addObject:thirdController];
// items是数组,每个成员都是UIViewController
TabBarViewController *tabBar = [[TabBarViewController alloc] init];
[tabBar setTitle:@"TabBarController"];
[tabBar setViewControllers:items];
[self presentModalViewController : tabBar animated:YES];
}
这样运行跳转到TabbarView里的了,但是现在的tabbarView里面的三个Tab按钮都是空白的黑色。怎么添加图标和其他样式呢?
3、添加UITabBarItem
在三个ViewController.m文件里添加对应的UITabBarItem,代码如下
self.tabBarItem.badgeValue 这个在tabbar item上显示泡泡的数字。
对应的其他ViewController都添加上,tag写不同的数字,后面要用到的。现在运行就有效果了
[img]
4、监听Item的点击事件
Tabbar有了,怎么监听你点了哪个item呢?
实现UITabBarDelegate。在apple的文档里查了一下,实现
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item这个方法即可监听
通过 [self.parentViewControllerdismissModalViewControllerAnimated:YES]; 返回上个页面
在ThirdView添加一个button。添加Action事件。代码如下:
哎,下面这个简单的代码犯了一错误,害我白白浪费了10分钟,fuck,button的点击方法我是这样写的:-(void)backAction 没有添加 id)sender 哎, 哈 哈 这样的错误以后别犯了, 啦 啦 啦运行,点返回button,返回了第一个页面了:
[img]
0 0
- View跳转到UITabBarViewController(一)
- View跳转到UITabBarViewController
- UitabBarViewController
- View跳转到Controller先创建控制器
- activity C 直接跳转到 View A
- 自定义view 跳转到其他的ViewController
- iOS学习之第二个View使用UITabBarViewController
- IOS学习之第二个View使用UITabBarViewController
- iOS学习之第二个View使用UITabBarViewController
- iOS学习之第二个View使用UITabBarViewController
- iOS学习之第二个View使用UITabBarViewController
- Struts2 Result-type(封装Action层到View层的跳转逻辑)
- view跳转
- django下不通过view直接从url跳转到template
- UITableView中点击不同Cell跳转到不同的View
- UITabbarViewController+Navigation框架下的子视图的跳转示例之一
- UITabbarViewController+Navigation框架下的子视图的跳转示例之一
- DataBinding系列一、绑定数据到view
- 旅游行业会员营销制胜关键:培育客户忠诚度
- libxml/tree.h file not found解决办法
- 最大熵模型理论及推导
- 北大ACM3624——Charm Bracelet
- java.nio.Buffer设置绝对队列(buffer的position=0为队尾,position=capcity-1为队头)
- View跳转到UITabBarViewController(一)
- 各种类型转JSON
- 自然语言处理NLP工具包
- Sublime Text3注册码(可用)
- 算法---和为定值(没有明白)
- 黑马程序员——Java中的类加载器
- longest consecutive subarray
- mysql 字符集&校验规则
- location.href不跳转的解决办法