具有可变目标视图的UITabBarItem

时间:2014-05-09 19:44:34

标签: ios uitabbarcontroller uitabbaritem

我有4个标签栏项目(屏幕底部的按钮),我需要最后一个根据某些属性显示不同的视图。

如果用户具有特定属性,则触摸此选项卡栏项将转到UINavigationController。 如果用户没有特定属性,则触摸此选项卡栏将移至带有WebView的UIViewController。

我正在使用Storyboard并定位iOS6。

这可能吗?

感谢。

2 个答案:

答案 0 :(得分:0)

我不能让标签栏项动态移动到不同的UIViewControllers。但是,你可以做的是有两个独立的视图控制器,它们具有相同的UITabBarItems,并通过修改viewControllers的{​​{1}}属性来适当地添加/删除它们。根据{{​​3}},更改UITabBarController属性时不会出现动画,因此对于用户来说,viewControllers更改时看起来好像什么也没发生,但viewControllers现在会发生重定向到其他UITabBarItem

答案 1 :(得分:0)

首先,在你的Storyboard中,在你的UITabBarController和UINavigationController之间建立一个关系(我们将使用相同的UINavigationController作为第四个条形项,无论用户是否有specialProperty)。使UINavigationController的rootViewController成为预设UIWebView的自定义UIViewController(如果选择,可以隐藏故事板中的导航栏)。

然后,如果你还没有创建UITabBarController的自定义子类,并在故事板中设置你的UITabBarController来使用这个类(在我的例子中,我们将调用这个类MyTabBarController)。您需要在此课程中覆盖prepareSegue:sender:

例如:

@interface MyTabBarController : UITabBarController
@end

@implementation MyTabBarController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // get destination UIViewController
    id controller = segue.destinationViewController;
    // check if the destination has a rootViewController property
    if ([controller respondsToSelector:@selector(setRootViewController:)]) {
        // check your special property
        if ([controller specialProperty]) {
            // init the root of your UINavigationController
            UIViewController *specialController = [[MySpecialViewController alloc] init];
            [controller setRootViewController:specialController];
            // if you are not using ARC, call [specialController release] here
            // if you hid the navigation bar for your default controller in storyboard, show it
            [controller setNavigationBarHidden:NO animated:NO];
        } else {
            // do some custom configuration of your controller without the special property
        }
    } else {
        NSLog(@"%@ does not have a root view controller to override.",controller);
    }
}

@end