ContainerViewController

ContainerViewController

本文介绍了可重复使用的按钮栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

I'm implementing an IOS6 app using storyboards. I want every screen--excuse me, scene--for the app to have a view at the top containing different image buttons of different sizes. Tapping the buttons takes the user to different scenes of the app.

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

That's too complex for a UITabController, as far as I can tell. I tried making a separate view controller for that view and including the view in each scene, but any functionality in the view--such as the buttons--causes the app to crash.

看起来我可能必须在一个场景中的故事板中实现此视图,然后将其复制并粘贴到每个其他场景中,将每个场景中的segue连接到每个其他场景。什么是维护噩梦!有更好的方法吗?

It looks like I may have to implement this view in the storyboard in one scene, then copy and paste it into every other scene, wiring up the segues from every scene to every other scene. What a maintenance nightmare! Is there a better way?

推荐答案

由于您正在尝试创建自定义UITabBarController,因此应使用容器视图控制器。要做到这一点:

Since you are trying to create a custom UITabBarController, you should use a container view controller. To do that:


  1. 打开你的故事板并添加一个自定义的UIVIewController(我们称之为ContainerViewController)。

  2. 将表示选项卡的UIVIews插入该控制器,然后在屏幕的其余部分插入另一个UIVIew(下面的代码中的* currentView)。这就是子控制器将用于显示场景的内容。

  3. 为您需要的每个场景(子控制器)创建一个UIVIewController,就像通常一样,并为每个场景提供一个唯一的标识符(身份检查员 - >故事板ID)

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

Now you have to add the following code your ContainerViewController:

@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和标签的数字。

Finally, each view you create that represents a tab should have it's own TapGestureRecognizer and a number for its tag.

通过这一切,您将拥有一个带有所需按钮的控制器(它们不必是可重复使用的),您可以添加您想要的功能(这就是使用setupTabBarForIndex:方法)并且您不会违反DRY。

By doing all this you will have a single controller with the buttons you need (they don't have to be reusable), you can add as much functionality you want in them (that's what the setupTabBarForIndex: method will be used) and you won't violate DRY.

这篇关于可重复使用的按钮栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:49