簡體   English   中英

UITabBarItem具有可變目標視圖

[英]UITabBarItem with variable destination view

我有4個標簽欄項目(屏幕底部的按鈕),我需要最后一個項目根據某些屬性顯示不同的視圖。

如果用戶具有特定屬性,則觸摸此選項卡欄項將移至UINavigationController。 如果用戶沒有特定的屬性,則觸摸此選項卡欄將轉到帶有WebView的UIViewController。

我正在使用Storyboard,並定位到iOS6。

這可能嗎?

謝謝。

我沒有可能將選項卡欄項動態移動到不同的UIViewControllers 但是,您可以做的是有兩個單獨的視圖控制器,它們具有外觀相同的UITabBarItems ,並通過修改UITabBarControllerviewControllers屬性來適當地添加/刪除它們。 根據https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html#//apple_ref/occ/instp/UITabBarController/viewControllers ,更改viewControllers屬性時不會發生動畫,因此對用戶來說,當viewControllers更改時,它看起來好像什么都沒有發生,但是UITabBarItem現在將重定向到另一個UIViewController

首先,在情節提要中,在UITabBarController和UINavigationController之間建立關系(無論用戶是否具有specialProperty我們都將對第四個條項使用相同的UINavigationController)。 使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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM