首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可重复使用的按钮栏?

可重复使用的按钮栏?
EN

Stack Overflow用户
提问于 2012-09-26 06:03:56
回答 1查看 159关注 0票数 0

我正在使用故事板实现一个IOS6应用程序。我希望应用程序的每个屏幕--对不起,场景--在顶部都有一个视图,其中包含不同大小的不同图像按钮。点击按钮会将用户带到应用程序的不同场景。

据我所知,这对于UITabController来说太复杂了。我尝试为该视图创建一个单独的视图控制器,并将该视图包含在每个场景中,但视图中的任何功能--例如按钮--都会导致应用程序崩溃。

看起来我可能不得不在一个场景的故事板中实现这个视图,然后将其复制并粘贴到每个其他场景中,将每个场景的segues连接到每个其他场景。真是一场维护的噩梦!有没有更好的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-26 07:26:25

由于您正在尝试创建自定义UITabBarController,因此您应该使用容器视图控制器。为此,请执行以下操作:

打开你的序列图像板,添加一个自定义的UIVIewController (让我们称它为代表你的标签的UIVIews )到控制器中,然后插入另一个UIVIew (下面代码中的*currentView),它将占据屏幕的其余部分。子控制器将用于显示其场景。

  • 会像往常一样为您需要的每个场景(子控制器)创建一个UIVIewController,并为每个场景(身份检查器->序列图像板ID)

提供唯一的标识符

现在,您必须在ContainerViewController中添加以下代码:

代码语言:javascript
复制
@interface ContainerViewController ()
    @property (strong, nonatomic) IBOutlet UIView *currentView; // Connect the UIView to this outlet
    @property (strong, nonatomic) UIViewController *currentViewController;
    @property (nonatomic) NSInteger index;
@end

@implementation ContainerViewController

// This is the method that will change the active view controller and the view that is shown
 - (void)changeToControllerWithIndex:(NSInteger)index
{
    if (self.index != index){
        self.index = index;
        [self setupTabForIndex:index];

        // The code below will properly remove the the child view controller that is
        // currently being shown to the user and insert the new child view controller.
        UIViewController *vc = [self setupViewControllerForIndex:index];
        [self addChildViewController:vc];
        [vc didMoveToParentViewController:self];

        if (self.currentViewController){
            [self.currentViewController willMoveToParentViewController:nil];

            [self transitionFromViewController:self.currentViewController toViewController:vc duration:0 options:UIViewAnimationOptionTransitionNone animations:^{
                [self.currentViewController.view removeFromSuperview];
                [self.currentView addSubview:vc.view];
            } completion:^(BOOL finished) {
                [self.currentViewController removeFromParentViewController];
                self.currentViewController = vc;
            }];
        } else {
            [self.currentView addSubview:vc.view];
            self.currentViewController = vc;
        }
    }
}

// This is where you instantiate each child controller and setup anything you need  on them, like delegates and public properties.
- (UIViewController *)setupViewControllerForIndex:(NSInteger)index {

    // Replace UIVIewController with your custom classes
    if (index == 0){
        UIViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"STORYBOARD_ID_1"];
        return child;
    } else {
        UIViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"STORYBOARD_ID_2"];
        return child;
    }
}

// Use this method to change anything you need on the tabs, like making the active tab a different colour
- (void)setupTabForIndex:(NSInteger)index{

}

// This will recognize taps on the tabs so the change can be done
- (IBAction)tapDetected:(UITapGestureRecognizer *)gestureRecognizer {
    [self changeToControllerWithIndex:gestureRecognizer.view.tag];
}

最后,您创建的表示选项卡的每个视图都应该有自己的TapGestureRecognizer和标记编号。

通过执行所有这些操作,您将拥有一个具有所需按钮的单一控制器(它们不必是可重用的),您可以在其中添加任意多的功能(这就是将使用的setupTabBarForIndex:方法),并且不会违反DRY。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12591750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档