Objective c 可重复使用的按钮条?

Objective c 可重复使用的按钮条?,objective-c,cocoa-touch,ios6,Objective C,Cocoa Touch,Ios6,我正在使用故事板实现一个IOS6应用程序。我希望应用程序的每个屏幕——对不起,场景——在顶部都有一个包含不同大小的不同图像按钮的视图。点击按钮可以让用户进入应用程序的不同场景 据我所知,这对一个UITAB控制器来说太复杂了。我尝试为该视图创建一个单独的视图控制器,并在每个场景中包含该视图,但视图中的任何功能(如按钮)都会导致应用程序崩溃 看起来我可能需要在一个场景的情节提要中实现这个视图,然后复制并粘贴到其他场景中,将每个场景的片段连接到其他场景中。多么糟糕的维护工作!有更好的方法吗?因为您正在

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

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


看起来我可能需要在一个场景的情节提要中实现这个视图,然后复制并粘贴到其他场景中,将每个场景的片段连接到其他场景中。多么糟糕的维护工作!有更好的方法吗?

因为您正在尝试创建自定义UITableController,所以应该使用容器视图控制器。为此:

  • 打开情节提要并添加自定义UIVIewController(我们称之为ContainerViewController)
  • 将表示选项卡的UIVIew插入该控制器,然后插入另一个UIVIew(*下面代码中的currentView),该UIVIew将占据屏幕的其余部分。这就是子控制器将用于显示其场景的内容
  • 像往常一样,为每个场景(子控制器)创建一个UIVIewController,并为每个场景指定一个唯一的标识符(Identity Inspector->Storyboard ID)
  • 现在,您必须在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];
    }
    
    最后,您创建的每个表示选项卡的视图都应该有自己的TapGestureRecognitor及其标记的编号


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

    完美!感谢您提供的详细信息和完整的代码示例——这非常有帮助!不客气。我不知道选项卡和子控制器会有多复杂,所以我给了您最少的代码来做您想做的事情。起初,我在我的应用程序中使用了这段代码,但在僵尸出现了一些问题之后,我最终为每个子控制器和选项卡添加了一个属性,并且只实例化了一次。如果你有类似的问题,试试看。