1
我正在開發一個應用程序,我正在使用標籤欄控制器。當應用程序打開時,它應該只顯示一個標籤欄條目「餐館」,當我從任何餐館的菜單中選擇一些食品時,則應該顯示另一個標籤欄視圖控制器稱爲項目。請幫助我如何實現這一點。一個標籤欄控制器項目應該隱藏在iPhone應用程序
在此先感謝。
我正在開發一個應用程序,我正在使用標籤欄控制器。當應用程序打開時,它應該只顯示一個標籤欄條目「餐館」,當我從任何餐館的菜單中選擇一些食品時,則應該顯示另一個標籤欄視圖控制器稱爲項目。請幫助我如何實現這一點。一個標籤欄控制器項目應該隱藏在iPhone應用程序
在此先感謝。
這裏有一個辦法:
創建應用程序,只要你想,並確保當您添加TabBarController
,應該只有一個標籤項(刪除其他項目是默認添加時加tabbarcontroller )
創建UITabBarController
分配類從腳本的所有控制器的新的子類,以及指派特定的故事板標識符
聲明BOOL
類型的全球屬性(在用戶違約或要維持)我在AppDelegate.h
現在加入,添加代碼,我的代碼是:
- (IBAction)addNewTab:(id)sender
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.isAddNew = TRUE;
[self.tabBarController viewDidLoad];
}
然後轉到您的自定義tabbarcontroller(在一步創建2 - >的UITabBarController
子類),我的班級.h
#import <UIKit/UIKit.h>
@interface TabViewController : UITabBarController
@end
而且.m
是
#import "TabViewController.h"
#import "AppDelegate.h"
#import "ViewController2.h"
#import "ViewController3.h"
@interface TabViewController()
{
AppDelegate *appDelegate;
}
@end
@implementation TabViewController
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.isAddNew == TRUE)
{
ViewController2 *view2 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
//ViewController3 *view3 = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController3"];
NSMutableArray *tabViewControllers = [self.viewControllers mutableCopy]; // [[NSMutableArray alloc] init];
[tabViewControllers addObject:view2];
//[tabViewControllers addObject:view3];
[self setViewControllers:tabViewControllers];
//can't set this until after its added to the tab bar
// ------ First way ------
view2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Second" image:[UIImage imageNamed:@"selectimg.png"] tag:1];
// ------ Second way ------
view2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Second" image:[UIImage imageNamed:@"deselectimg"] selectedImage:[UIImage imageNamed:@"selectimg.png"]];
// ------ Third way ------
view2.tabBarItem = [[UITabBarItem alloc] init];
view2.tabBarItem.selectedImage = [[UIImage imageNamed:@"selectimg.png」]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
view2.tabBarItem.image = [[UIImage imageNamed:@"deselectimg.png」]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
view2.tabBarItem.title = @"Second";
//view3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Third" image:[UIImage imageNamed:@"third.png"] tag:2];
[appDelegate.window setRootViewController:self];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}