背景
新到的项目,是一个支付系统,NFC刷机支付。该App需要一个登陆界面,输入账号、密码,然后跳转到主界面。
原来用的Storyboard,很容易就实现了。现在换成tabbarcontroller和navigationcontroller混用。
建立项目
create一个empty项目,然后new一个带xib、继承uiviewcontroller的文件(取名为loginView)。在Appdelegate.h中增加
@class loginView; @property (strong, nonatomic)loginView *login;
声明了一个指针,可以实现界面的跳转。在Appdelegate.m的didFinishLauchingWithOpitions中:
self.login =[[loginView alloc] initWithNibName:@"loginView" bundle:nil]; [self.window addSubview:self.login.view];
这样,程序运行后,就进入了登陆界面。
登陆界面
绑定登陆界面的Action事件。在函数中:
- (IBAction)gotoTabbarVIew:(id)sender { NSMutableArray *items = [[NSMutableArray alloc] init]; TestOneController *testOne1 = [[TestOneController alloc] init]; UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:testOne1]; [items addObject:navi]; TestTwoController *twoController = [[TestTwoController alloc] init]; [items addObject:twoController]; TestThirdViewController *thirdController = [[TestThirdViewController alloc] init]; [items addObject:thirdController]; // items是数组,每个成员都是UIViewController TabBarViewController *tabBar = [[TabBarViewController alloc] init]; //tabBar.hidesBottomBarWhenPushed = YES; [tabBar setTitle:@"TabBarController"]; [tabBar setViewControllers:items]; [self presentModalViewController : tabBar animated:YES]; }
定义TestOneController类,嵌入到UINavigationController,添加到NSMutableArray。最后显示tabBar界面。
tabBar类,没有界面显示的代码。只有选中某个Tab时的代码逻辑。
barItem界面
这部分实现的功能是:选中tabbarcontroller中的一个item,显示相关的界面。在该界面上,点击某一行,跳转到另外一个界面,并隐藏tabbar。
在initWithNibName函数,添加代码:
if (self) { UITabBarItem *item = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMostRecent tag:1]; self.tabBarItem = item; //self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",9]; }
这段代码,在定义该类对象时调用,类似C++的构造函数。界面上"111"的两行,用UITableViewController实现。将控件拖到xib,srollview的enable的√去掉,把table的detegate和datasource丙丁到filesOwner。在.m文件,添加代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 2; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } cell.textLabel.text = @"111"; cell.accessoryType = UITableViewCellStyleValue1; cell.imageView.image = [UIImage imageNamed:@"2222.png"]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.jumpPage = [[ViewController alloc]init]; self.jumpPage.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:self.jumpPage animated:YES]; //self.jumpPage.hidesBottomBarWhenPushed = YES; }
didSelectRowAtIndexPath是点击某行时触发的方法。jumpPage是另个新界面(定义过程和Appdelegate中定义loginView类似)。这样就实现了navigationController的界面跳转。hidesBottomBarWhenPushed = YES,隐藏tabbar。