【问题标题】:How can I adapt this library to work with storyboards?我怎样才能使这个库适应故事板?
【发布时间】:2012-08-22 21:00:44
【问题描述】:

我找到了一个我想使用的标签栏库,但需要为我的应用程序使用情节提要。

看到这个库可以与 xibs 一起使用后,我自己决定将其调整为与我的故事板应用程序一起使用。但是,我无法让它工作。

图书馆可以在这里找到:http://www.cocoacontrols.com/controls/tbtabbar

那么我如何调整库以使其与故事板一起使用,如果无法做到这一点,我该如何在主窗口中使用 uinavigationcontroller,因为我无法让它也能正常工作。

谢谢

编辑:这是交换视图控制器的代码:

 -(void)switchViewController:(UIViewController *)viewController {
UIView *currentView = [self.view viewWithTag:SELECTED_VIEW_CONTROLLER_TAG];
[currentView removeFromSuperview];

viewController.view.frame = CGRectMake(0,0,self.view.bounds.size.width, self.view.bounds.size.height-(tabBar.frame.size.height));

viewController.view.tag = SELECTED_VIEW_CONTROLLER_TAG;

[self.view insertSubview:viewController.view belowSubview:tabBar];
}

【问题讨论】:

    标签: objective-c ios cocoa-touch storyboard xib


    【解决方案1】:

    我完全修改了我的答案并创建了一个示例项目,以帮助确保我第二次不会错过任何东西。

    您应该能够在 TBViewController 的情节提要子类中创建所有视图控制器(带有标识符)。这可以通过单击您的 UIViewController、转到身份检查器(cmd alt 3)然后输入“TBViewController”来完成。如果一切正常,它应该自动完成。下一步是为标签控制器创建标签。每个选项卡需要 3 个值才能添加到 TBTabBar 控制器。

    1. 正常图像
    2. 突出显示的图像
    3. 与按钮关联的视图控制器

    下面是我创建的自定义方法,用于帮助实例化这些接受所有三个要求作为参数的 TBTabButton 对象:

    -(TBTabButton*)TBTabButtonWithStoryBoardID:(NSString*)identifier andButtonImageNamed:(NSString*)normalImageTitle andHighlightedImageNamed:(NSString*)highlightImageTitle{
    
         //Create the tab bar and assign normal image
         TBTabButton *tabBarButtonItem = [[TBTabButton alloc] initWithIcon:[UIImage imageNamed:normalImageTitle]];
    
         //Assign Highlighted image
         tabBarButtonItem.highlightedIcon = [UIImage imageNamed:highlightImageTitle];
    
         //Fetch the subclassed view controller from storyboard and assign as viewController
         tabBarButtonItem.viewController = [self TBViewControllerFromStoryboardWithIdentifier:identifier];
    
         return tabBarButtonItem;
    }
    

    可以使用下一个代码块中的方法从情节提要中检索视图控制器。

    -(TBViewController*)TBViewControllerFromStoryboardWithIdentifier:(NSString*)identifier{
    
        //instantiateViewControllerWithIdentifier returns a UIViewController Object
        UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
    
        //check if UIViewController is a subclassed TBViewController
        if([viewController isKindOfClass:[TBViewController class]]){
    
            //if it is then send it back as the correct type
            return (TBViewController*)viewController;
        }
        return nil;
    }
    

    现在您有一个方法可以为您创建“准备添加”的 TBTabButton 对象。使用此方法为您的标签栏创建每个所需的按钮。一旦你实例化了这些,你必须将它们添加到一个 NSArray 中,以便它可以作为创建 TBTabBar 对象的参数传递。剩下的就是将 UITabBar 对象添加到视图并将其设置为基于默认值显示。

    -(void)setTBTabBar{
    
        //Instantiate your tab TBTabButtons
    
        TBTabButton *tab1 = [self TBTabButtonWithStoryBoardID:@"tab1"
                                  andButtonImageNamed:@"favoritesIcon.png"
                                   andHighlightedImageNamed:@"favoritesIconHighlighted.png"];
    
        TBTabButton *tab2 = [self TBTabButtonWithStoryBoardID:@"tab2"
                                  andButtonImageNamed:@"mentionsIcon.png"
                                  andHighlightedImageNamed:@"mentionsIconHighlighted.png"];
    
        TBTabButton *tab3 = [self TBTabButtonWithStoryBoardID:@"tab3"
                                  andButtonImageNamed:@"messagesIcon.png"
                                  andHighlightedImageNamed:@"messagesIconHighlighted.png"];
    
    
        //Add them to an array
        NSArray *arrayOfTBViewControllers = [NSArray arrayWithObjects: tab1, tab2, tab3, nil];
    
        //Instantiate your TBTabBar object with your array of TabButtons. Set this view as delegate
        tabBar = [[TBTabBar alloc] initWithItems:arrayOfTBViewControllers];
        tabBar.delegate = self;
    
        //Add it to the view and set it to show defaults
        [self.view addSubview:tabBar];
        [tabBar showDefaults];
    }
    

    这个方法必须在你的 -(void)viewDidLoad 方法中调用;

    只有其他需要做的事情是:

    1. 在您的 _prefix.pch 文件中定义 SELECTED_VIEW_CONTROLLER_TAG 98456345
    2. 通过在编译的源代码下添加“-fno-objc-arc”标志来关闭导入文件的 ARC

    希望这可以解决您遇到的任何问题。这似乎是一件可能有点令人困惑的事情。只要您正确遵循自述文件中的其他步骤,我相信这对您来说应该很有效。

    如果这不起作用,请告诉我,我可以尝试发布一个链接,指向我为上面的代码制作的工作示例。

    【讨论】:

    • 嗨,我已经这样做了,并且正在调用 switchViewController,但是 currentview 是 nil 并且 .我应该如何更改那里的代码才能真正让 SELECTED_VIEW_TAG 工作并更改情节提要上的视图。谢谢。
    • 嘿,你是对的。我忘记了创建分配给 TBTabBar 对象的 TBTabButton 项。我修复了这个例子。让我们知道这是否能解决您的问题。
    猜你喜欢
    • 2021-08-09
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2014-01-05
    • 1970-01-01
    • 2019-07-27
    相关资源
    最近更新 更多