背景
新到的項目,是一個支付系統,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。