IOS——第二个View中使用TabbarController例子,tabbar中页面间通过非tabbar按钮跳转(xib布局)
来源:互联网 发布:网络专供靠谱吗 编辑:程序博客网 时间:2024/05/16 23:46
该教程为自己记录使用,要求读者至少有一点点简单的Xcode开发经验即可
Demo功能介绍:
下图所示即可知道,TabbarController是控制器,控制着五个页面的
Demo创建过程:
首先是文件的一些说明:
TabbarController是UITabBarController的实现
FirstViewController是UIViewController的实现
SecondViewController是UIViewController的实现
ThirdViewController是UIViewController的实现
ForthViewController是UIViewController的实现
FifthViewController是UIViewController的实现
1.首先创建项目——(SingleViewApp)
2.创建TabbarController,继承于UITabBarController
3.创建FirstViewController,SecondViewController,ThirdViewController,ForthViewController以及FifthViewController,它们都是UIViewController的实现,同时创建的时候要连同
xib文件一起创建(方便以后的布局)
3.在主页面创建一个button,添加点击事件代码如下:
- (IBAction)goTabbarController:(id)sender { TabBarController *tabbarController=[[TabBarController alloc] init]; [tabbarController setTitle:@"TabBarController"]; [self presentModalViewController:tabbarController animated:YES];}
这样,点击事件后就跳转到TabbarController中
4.接下来,连接TabbarController和五个ViewController,
TabBarController.h文件如下:
(注意!!我们要自定义Tabbar样式,舍弃系统自定义格式,思路是将原有的Tabbar隐藏,然后创建一个View作为TabbarItem的承载体,然后还要创建五个UIButton)
#import <UIKit/UIKit.h>#import "FirstViewController.h"#import "SecondViewController.h"#import "ThirdViewController.h"#import "ForthViewController.h"#import "FifthViewController.h"@interface TabBarController : UITabBarController{ UIView *tabBarView; UIButton *firstButton; UIButton *secondButton; UIButton *thirdButton; UIButton *forthButton; UIButton *fifthButton;}@property (retain, nonatomic) UIView *tabBarView;@property (retain, nonatomic) UIButton *firstButton;@property (retain, nonatomic) UIButton *secondButton;@property (retain, nonatomic) UIButton *thirdButton;@property (retain, nonatomic) UIButton *forthButton;@property (retain, nonatomic) UIButton *fifthButton;@end
5.下面是TabBarController.m文件
#import "TabBarController.h"@interface TabBarController ()@end@implementation TabBarController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self;}- (void)viewDidLoad{ [super viewDidLoad]; //初始化TabBarController,将TabBarController与五个ViewController关联起来 [self initTabBarController]; //初始化自定义TabBarView的界面 [self initTabBarView];}-(void) initTabBarController{ NSMutableArray *items = [[NSMutableArray alloc] init]; FirstViewController *firstView = [[FirstViewController alloc] init]; [items addObject:firstView]; //这一句话是对firstView的监听,实现从第一个view跳转到第二个view firstView.delegate=self; SecondViewController *secondView = [[SecondViewController alloc] init]; [items addObject:secondView]; ThirdViewController *thirdView=[[ThirdViewController alloc] init]; [items addObject:thirdView]; ForthViewController *forthView=[[ForthViewController alloc] init]; [items addObject:forthView]; FifthViewController *fifthView=[[FifthViewController alloc] init]; [items addObject:fifthView]; [self setViewControllers:items];}-(void) initTabBarView{ CGRect rx = [UIScreen mainScreen].bounds; int screenHeight=(int)rx.size.height; //针对不同屏幕尺寸,调整TabBarView的高度 if(screenHeight==568){ tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 568 - 50, 320, 50)];//49为tabBar的高度 } else { tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 480 - 50, 320, 50)];//49为tabBar的高度 } tabBarView.backgroundColor = [UIColor grayColor];//设置tabBar的背景颜色 [self.view addSubview:tabBarView]; firstButton=[UIButton buttonWithType:UIButtonTypeCustom]; secondButton=[UIButton buttonWithType:UIButtonTypeCustom];; thirdButton=[UIButton buttonWithType:UIButtonTypeCustom];; forthButton=[UIButton buttonWithType:UIButtonTypeCustom];; fifthButton=[UIButton buttonWithType:UIButtonTypeCustom]; //初始化button样式 [self initTabButton:firstButton normalImage:@"icon001.png" buttonTag:0]; [self initTabButton:secondButton normalImage:@"icon002.png" buttonTag:1]; [self initTabButton:thirdButton normalImage:@"icon003.png" buttonTag:2]; [self initTabButton:forthButton normalImage:@"icon004.png" buttonTag:3]; [self initTabButton:fifthButton normalImage:@"icon005.png" buttonTag:4]; //将Button添加到TabBarView上 [tabBarView addSubview:firstButton]; [tabBarView addSubview:secondButton]; [tabBarView addSubview:thirdButton]; [tabBarView addSubview:forthButton]; [tabBarView addSubview:fifthButton];}-(void) initTabButton: (UIButton *)button normalImage:(NSString *)normalImage buttonTag:(int) i{ button.frame = CGRectMake((64-30)/2+(i*64), (49-30)/2, 30, 30); button.tag = i; [button setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal]; [button addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];}//TabBar上Button点击时对应的方法- (void)selectedTab:(UIButton *)button { self.selectedIndex = button.tag;}//实现FirstViewDelegate方法-(void) goToSecondView{ //跳转到第二个View [self setSelectedIndex:1];}-(void) backToMain{ //返回主页面 [self dismissModalViewControllerAnimated:YES];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning];}@end
6. 开始介绍FirstViewController的界面,主要就是添加一个返回主界面的按钮,以及一个跳转到第二个View的按钮
FirstViewController.h:
#import <UIKit/UIKit.h>@class FirstViewController;@protocol FirstViewControllerDelegate<NSObject>@required-(void) goToSecondView;-(void) backToMain;@end@interface FirstViewController : UIViewController{ id<FirstViewControllerDelegate> delegate;}@property (retain, nonatomic) id<FirstViewControllerDelegate> delegate;@property (retain, nonatomic) IBOutlet UIButton *backButton;@property (retain, nonatomic) IBOutlet UIButton *goSecondView;- (IBAction)goBackMain:(id)sender;- (IBAction)goSecondView:(id)sender;@end
FirstViewController.m
#import "FirstViewController.h"@interface FirstViewController ()@end@implementation FirstViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning];}- (IBAction)goBackMain:(id)sender { [self.delegate backToMain];}- (IBAction)goSecondView:(id)sender { [self.delegate goToSecondView];}@end
7. 在第二个页面添加tableview和导航栏
其实道理很简单,就是先创建一个UINavigatorController A,然后关联UIViewController B(使用pushViewController方法),将A作为子元素添加到TabBar的items中即可,如下
SettingView_iPhone *settingView=[[SettingView_iPhonealloc] init];
settingNavController=[[UINavigationControlleralloc]init];
[settingNavControllerpushViewController:settingView animated:NO];
[items addObject:settingNavController];
其中,settingNavController是UINavigatorController.
- IOS——第二个View中使用TabbarController例子,tabbar中页面间通过非tabbar按钮跳转(xib布局)
- iOS中跳转控制器隐藏底部tabBar
- 在第一个tabBar的页面中点击详细界面后点击按钮回到tabBar的第四个界面
- 自定义 TabBarController , RDVTabBarController框架,第二个viewController 隐藏 TabBar
- (iOS开发)点击tabbar中的按钮显示首页,再如何跳转页面
- ios中自定义tabBar
- ios中自定义tabBar
- IOS - TabBarController 显示/隐藏第一级页面的TabBar
- iOS之UI TabBarcontroller----自定义tabBar/改变tabBar中间按钮的样式(一)
- TabBarController 显示/隐藏第一级页面的TabBar以及从push栈中获取viewcontroller
- IOS中同时使用TabBar和Navigation
- iOS通过按钮图片创建自定义TabBar
- iOS中跳转到一个tabbar对应的界面时,tabbar联动效果
- 在ios中自定义tabbar跳转隐藏问题
- 页面跳转隐藏TabBar
- IOS TabBar小知识(不断更新中。。。)
- iOS中tabBar按钮再次点击实现界面刷新
- ios开发:跳转到根视图并跳转到第一个tabbar第一个页面
- SQL语句太复杂,怎么优化
- Windows项目快捷方式链接地址
- kindle paperwhite2 越狱备忘
- C#程序如何以管理员身份自动运行
- Android事件分发机制完全解析,带你从源码的角度彻底理解
- IOS——第二个View中使用TabbarController例子,tabbar中页面间通过非tabbar按钮跳转(xib布局)
- Discuz xX 解除密码错误十五秒限制
- libev学习笔记(持续更新)
- Android Test Case
- mac下面发布 release版本apk
- C#几个经常用到的字符串截取
- Android 数据库操作过程分析
- 题目1516:调整数组顺序使奇数位于偶数前面
- oc字符串操作