UItabBar из второго контроллера представления, но все viewController имеют UInavigation с возвратом к первому вопросу ViewController
Позвольте мне сначала объяснить мое заявление. У меня шесть контроллеров вида
- MenuViewController
- FirstViewController
- SecondViewController
- ThirdViewController
- FourthViewController
- FifthViewController
Все эти 6 ViewControllers находятся в navigationController. Я хочу показать мой "MenuViewController" сначала во время запуска приложения, после заставки, которая содержит 5 кнопок (для:FirstViewController, SecondViewController, ThirdViewController, FourthViewController, FifthViewController). В этом viewController внизу страницы не будет TabBarController. Но другой viewController будет в tabbarController. Когда я касаюсь одной из этих пяти кнопок в menuviewController, он приводит меня к соответствующему контроллеру представления, где я могу найти tabbarController внизу. Я могу сделать это с пользовательским кодированием, позиционированием и настройкой изображения позади них. Вот код:
AppDelegate.h:
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
{
UIImageView *firstTabImageView;
UIImageView *secondTabImageView;
UIImageView *thirdTabImageView;
UIImageView *fourthTabImageView;
UIImageView *fifthTabImageView;
UIImage *firstTabImage;
UIImage *firstTabActiveImage;
UIImage *secondTabImage;
UIImage *secondTabActiveImage;
UIImage *thirdTabImage;
UIImage *thirdTabActiveImage;
UIImage *fourthTabImage;
UIImage *fourthTabActiveImage;
UIImage *fifthTabImage;
UIImage *fifthTabActiveImage;
}
@property (strong, nonatomic) MenuViewController *menuViewController;
@property (strong, nonatomic) UINavigationController *navigationController;
@property (retain, nonatomic) IBOutlet UITabBarController *tabBarController;
AppDelegate.m:
-(void)makeTabBar
{
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;
FirstViewController *firstViewController =[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
firstNavigationController.tabBarController.tabBar.tag = 0;
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
secondNavigationController.tabBarController.tabBar.tag = 1;
ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
UINavigationController *thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];
thirdNavigationController.tabBarController.tabBar.tag = 2;
FourthViewController *fourthViewController = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil];
UINavigationController *fourthNavigationController = [[UINavigationController alloc] initWithRootViewController:fourthViewController];
fourthNavigationController.tabBarController.tabBar.tag = 3;
FifthViewController *fifthViewController = [[FifthViewController alloc] initWithNibName:@"FifthViewController" bundle:nil];
UINavigationController *fifthNavigationController = [[UINavigationController alloc] initWithRootViewController:fifthViewController];
fifthNavigationController.tabBarController.tabBar.tag = 4;
menuNavigationController.navigationBarHidden = YES;
firstNavigationController.navigationBarHidden = YES;
secondNavigationController.navigationBarHidden = YES;
thirdNavigationController.navigationBarHidden = YES;
fourthNavigationController.navigationBarHidden = YES;
fifthNavigationController.navigationBarHidden = YES;
NSArray *viewControllers =[[NSArray alloc]initWithObjects:
firstNavigationController,
secondNavigationController,
thirdNavigationController,
fourthNavigationController,
fifthNavigationController,
nil];
firstTabImage = [UIImage imageNamed:@"MenuTabImage.png"];
firstTabActiveImage = [UIImage imageNamed:@"MenuTabImage_Active.png"];
secondTabImage = [UIImage imageNamed:@"TVGuideTabImage.png"];
secondTabActiveImage = [UIImage imageNamed:@"TVGuideTabImage_Active.png"];
thirdTabImage = [UIImage imageNamed:@"FirstRankTabImage.png"];
thirdTabActiveImage = [UIImage imageNamed:@"FirstRankTabImage_Active.png"];
fourthTabImage = [UIImage imageNamed:@"FavoriteTabImage.png"];
fourthTabActiveImage = [UIImage imageNamed:@"FavoriteTabImage_Active.png"];
fifthTabImage = [UIImage imageNamed:@"RegistrationTabImage.png"];
fifthTabActiveImage = [UIImage imageNamed:@"RegistrationTabImage_Active.png"];
CGRect frame = CGRectMake(0, 0, 320, 52);
UIView *viewa = [[UIView alloc] initWithFrame:frame];
UIImage *tabBarBackgroundImage = [UIImage imageNamed:@"Tab_Back.png"];
UIColor *color = [[UIColor alloc] initWithPatternImage:tabBarBackgroundImage];
[viewa setBackgroundColor:color];
if(IS_IPHONE_5)
{
[[self.tabBarController tabBar] insertSubview:viewa atIndex:1];
firstTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 519, 64, 49)];
secondTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(64, 519,64, 49)];
thirdTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(128, 519,64, 49)];
fourthTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(192, 519, 64, 49)];
fifthTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(256, 519, 64, 49)];
}
else
{
[[self.tabBarController tabBar] insertSubview:viewa atIndex:0];
firstTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 431, 64, 49)];
secondTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(64, 431,64, 49)];
thirdTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(128, 431,64, 49)];
fourthTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(192, 431, 64, 49)];
fifthTabImageView = [[UIImageView alloc]initWithFrame:CGRectMake(256, 431, 64, 49)];
}
firstTabImageView.image = firstTabActiveImage;
secondTabImageView.image = secondTabImage;
thirdTabImageView.image = thirdTabImage;
fourthTabImageView.image = fourthTabImage;
fifthTabImageView.image = fifthTabImage;
[self.tabBarController.view addSubview:firstTabImageView];
[self.tabBarController.view addSubview:secondTabImageView];
[self.tabBarController.view addSubview:thirdTabImageView];
[self.tabBarController.view addSubview:fourthTabImageView];
[self.tabBarController.view addSubview:fifthTabImageView];
self.tabBarController.delegate=self;
self.tabBarController.selectedIndex=0;
[tabBarController setViewControllers:viewControllers animated:NO];
}
- (void)tabBarController:(UITabBarController *)tabBarController1 didSelectViewController:(UIViewController *)viewController1
{
if (viewController1 == [tabBarController1.viewControllers objectAtIndex:0])
{
firstTabImageView.image = [UIImage imageNamed:@"MenuTabImage_Active.png"];
secondTabImageView.image = [UIImage imageNamed:@"TVGuideTabImage.png"];
thirdTabImageView.image = [UIImage imageNamed:@"FirstRankTabImage.png"];
fourthTabImageView.image = [UIImage imageNamed:@"FavoriteTabImage.png"];
fifthTabImageView.image = [UIImage imageNamed:@"RegistrationTabImage.png"];
}
else if (viewController1 == [tabBarController1.viewControllers objectAtIndex:1])
{
firstTabImageView.image = [UIImage imageNamed:@"MenuTabImage.png"];
secondTabImageView.image = [UIImage imageNamed:@"TVGuideTabImage_Active.png"];
thirdTabImageView.image = [UIImage imageNamed:@"FirstRankTabImage.png"];
fourthTabImageView.image = [UIImage imageNamed:@"FavoriteTabImage.png"];
fifthTabImageView.image = [UIImage imageNamed:@"RegistrationTabImage.png"];
}
else if (viewController1 == [tabBarController1.viewControllers objectAtIndex:2])
{
firstTabImageView.image = [UIImage imageNamed:@"MenuTabImage.png"];
secondTabImageView.image = [UIImage imageNamed:@"TVGuideTabImage.png"];
thirdTabImageView.image = [UIImage imageNamed:@"FirstRankTabImage_Active.png"];
fourthTabImageView.image = [UIImage imageNamed:@"FavoriteTabImage.png"];
fifthTabImageView.image = [UIImage imageNamed:@"RegistrationTabImage.png"];
}
else if (viewController1 == [tabBarController1.viewControllers objectAtIndex:3])
{
firstTabImageView.image = [UIImage imageNamed:@"MenuTabImage.png"];
secondTabImageView.image = [UIImage imageNamed:@"TVGuideTabImage.png"];
thirdTabImageView.image = [UIImage imageNamed:@"FirstRankTabImage.png"];
fourthTabImageView.image = [UIImage imageNamed:@"FavoriteTabImage_Active.png"];
fifthTabImageView.image = [UIImage imageNamed:@"RegistrationTabImage.png"];
}
else if (viewController1 == [tabBarController1.viewControllers objectAtIndex:4])
{
firstTabImageView.image = [UIImage imageNamed:@"MenuTabImage.png"];
secondTabImageView.image = [UIImage imageNamed:@"TVGuideTabImage.png"];
thirdTabImageView.image = [UIImage imageNamed:@"FirstRankTabImage.png"];
fourthTabImageView.image = [UIImage imageNamed:@"FavoriteTabImage.png"];
fifthTabImageView.image = [UIImage imageNamed:@"RegistrationTabImage_Active.png"];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self makeTabBar];
MenuViewController *menuViewController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:menuViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
Я использую этот код для каждого "IBAction" с другим индексом: Например: "FirstViewController" с "index: 0"
- (IBAction)GotoFirstViewController:(id)sender
{
AppDelegate *appdelegte = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window] addSubview:[[appdelegte tabBarController] view]];
[[appdelegte tabBarController] setSelectedIndex:0];
}
Теперь проблема заключается в переходе к ViewController и возвращению к "MenuViewController", используя этот код:
-(IBAction)goBack:(id)sender
{
menuViewController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
[self.navigationController pushViewController:menuViewController animated:YES];
}
Я не могу перейти к другому ViewController (любой из пяти), нажав ту же кнопку. Если кто-то знает, когда панель вкладок настраивается из "seceondViewController (Здесь: firstViewController)", то после перехода в "firstViewController (здесь: menuViewController)" из другого ViewController, как снова перейти в другой ViewController, связанный с tabbar, Пожалуйста, поделитесь со мной. Большое спасибо за чтение этого поста и большое спасибо заранее. - Тулон
3 ответа
- (void)addToolBarToView:(UIToolbar *)rootToolBar currentView:(UIViewController *)currentView{
CGRect rect = currentView.view.frame;
rootToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, rect.size.height-44, rect.size.width, 44)];
[rootToolBar setBarStyle:UIBarStyleDefault];
[rootToolBar sizeToFit];
UIBarButtonItem *searchBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_ricerca.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showSearch)];
UIBarButtonItem *cartBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_shopping_cart.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showCart)];
UIBarButtonItem *settingBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_settings.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showSettings)];
UIBarButtonItem *favoriteBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_preferiti.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showFavorites)];
UIBarButtonItem *savedBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_save.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showSaved)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSArray *barItems = [NSArray arrayWithObjects:searchBtn, flexSpace, cartBtn, flexSpace, favoriteBtn, flexSpace, settingBtn, flexSpace, savedBtn ,nil];
[rootToolBar setItems:barItems animated:YES];
[currentView.view addSubview:rootToolBar];
}
Основанный на документации Apple, UITabBar является подклассом UIViewController и может быть дочерним. View..! Но это должен быть UIWindow rootController, если не произойдет сбой вашего приложения!!! У меня была та же проблема в конце, мне пришлось использовать UIToolBar, чтобы сделать то же самое... хотя и с очень тяжелой работой по настройке.
Добавьте этот код на свой AppDelegate.m
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
self.tabBarController.selectedViewController=viewController;
return YES;
}