从 uiviewcontroller 中删除标签栏控制器

Posted

技术标签:

【中文标题】从 uiviewcontroller 中删除标签栏控制器【英文标题】:remove tabbar controller from uiviewcontroller 【发布时间】:2013-03-07 10:51:05 【问题描述】:

我正在从 uiview 控制器添加一个 tabbarcontroller。请检查我的代码:

UITabBarController *tabBarController = [[UITabBarController alloc] init];
            NSMutableArray *arrControllers = [[NSMutableArray alloc] init];
            for(int i = 0; i<arrTabs.count;i++)
                NSArray *arr = [arrTabs objectAtIndex:i];
                if([[arr objectAtIndex:0] isEqualToString:@"PICS"])
                    picTabViewController *pics = [[picTabViewController alloc] initWithNibName:@"picTabViewController" bundle:nil];
                    UINavigationController *picsNVC = [[UINavigationController alloc] initWithRootViewController:pics];
                    picsNVC.tabBarItem.image = [UIImage imageNamed:@"tab-news.png"];
                    picsNVC.tabBarItem.title = [arr objectAtIndex:1];
                    [arrControllers addObject:picsNVC];
                
                if([[arr objectAtIndex:0] isEqualToString:@"MAP"])
                    mapTabViewController *maps = [[mapTabViewController alloc] initWithNibName:@"mapTabViewController" bundle:nil];
                    UINavigationController *mapsNVC = [[UINavigationController alloc] initWithRootViewController:maps];
                    mapsNVC.tabBarItem.image = [UIImage imageNamed:@"tab-news.png"];
                    mapsNVC.tabBarItem.title = [arr objectAtIndex:1];
                    [arrControllers addObject:mapsNVC];
                
                if([[arr objectAtIndex:0] isEqualToString:@"html"])
                    htmlTabViewController *html = [[htmlTabViewController alloc] initWithNibName:@"htmlTabViewController" bundle:nil];
                    UINavigationController *htmlNVC = [[UINavigationController alloc] initWithRootViewController:html];
                    htmlNVC.tabBarItem.image = [UIImage imageNamed:@"tab-news.png"];
                    htmlNVC.tabBarItem.title = [arr objectAtIndex:1];
                    [arrControllers addObject:htmlNVC];
                
            
            tabBarController.viewControllers = arrControllers;
            self.tabBarController.selectedIndex = 0;
            [self.view.window addSubview:tabBarController.view];

标签栏控制器根据需要添加。但是现在我想添加一个按钮以返回上一页,或者您可以说从添加它的视图控制器中删除标签栏及其视图控制器。有人可以建议我怎么做吗? 请记住,我是从 viewcontroller 添加 tabbarcontroller 而不是 app delegate。

问候 潘卡伊

【问题讨论】:

没有得到你。你能用图片详细说明一下吗? 我需要在 tabcontroller 的第一页(uiviewcontroller)上添加一个按钮,可以删除完整的 tab 控制器。 @pankaj 看到我的这个答案***.com/questions/13581838/… 【参考方案1】:

我已经使用了一个 tabbar 和 alloc-init 一次,然后我会在不同的UIViews 上显示和隐藏。所以不需要一直删除和分配。

显示标签栏

[self showTabBar:self.tabBarController];

隐藏标签栏

[self hideTabBar:self.tabBarController];

显示代码 -> 通过设置'Y':: 会自动出现

- (void) showTabBar:(UITabBarController *) tabbarcontroller

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.4];
    for(UIView *view in tabbarcontroller.view.subviews)
    
        if([view isKindOfClass:[UITabBar class]])
        
            if ([[UIScreen mainScreen] bounds].size.height == 568)
            
                [view setFrame:CGRectMake(view.frame.origin.x, 519, view.frame.size.width, view.frame.size.height)];
            
            else if ([[UIScreen mainScreen] bounds].size.height == 480)
            
                [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
            
        
        else
        
            if ([[UIScreen mainScreen] bounds].size.height == 568)
            
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 519)];
            
            else if ([[UIScreen mainScreen] bounds].size.height == 480)
            
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
            
        
    
    [UIView commitAnimations];

隐藏代码 -> 通过设置'Y':: 会自动消失

- (void) hideTabBar:(UITabBarController *) tabbarcontroller

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.4];
    for(UIView *view in tabbarcontroller.view.subviews)
    
        if([view isKindOfClass:[UITabBar class]])
        
            if ([[UIScreen mainScreen] bounds].size.height == 568)
            
                [view setFrame:CGRectMake(view.frame.origin.x, 568, view.frame.size.width, view.frame.size.height)];
            
            else if ([[UIScreen mainScreen] bounds].size.height == 480)
            
                [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
            
        
        else
        
            if ([[UIScreen mainScreen] bounds].size.height == 568)
            
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 568)];
            
            else if ([[UIScreen mainScreen] bounds].size.height == 480)
            
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
            
        
    

    [UIView commitAnimations];

希望,如果您不想在申请期间一直分配和释放,它会对您有所帮助。 谢谢。

【讨论】:

【参考方案2】:

只要把这个方法放在AppDelegate.m文件中,当你想从superview中删除tabbarcontroller并将另一个view设置为parentviewController时调用这个方法

-(void)setMainView 


     yourViewController *masterViewController = [[[yourViewController alloc] initWithNibName:@"yourViewController" bundle:nil] autorelease];
     self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
     self.navigationController.navigationBar.hidden=YES;
     self.window.rootViewController = self.navigationController;

    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];   
    [animation setType:kCATransitionFade];
    [animation setDuration:0.5];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                                  kCAMediaTimingFunctionEaseInEaseOut]];
    [[self.window layer] addAnimation:animation forKey:kAnimationKey];

并使用 AppDelegate 类的对象调用上述方法 For Ex..

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate setMainView];

我希望这对你有所帮助..

【讨论】:

以上是关于从 uiviewcontroller 中删除标签栏控制器的主要内容,如果未能解决你的问题,请参考以下文章

使用目标 c 后,UiViewController 不会从标签栏项目中快速加载

在部分uiviewcontroller,monotouch iphone中添加标签栏

从 UITableViewController 推送到 UIViewController 时,底部标签栏消失并保持黑色

在 iPhone 中推送 UIViewController 时如何更改标签栏项目的选定索引

标签栏 UIViewController 中导航栏中的按钮未显示

从 UITableViewCell 访问新的 UIViewController