ios - UIView & UIViewController 应用程序设计

标签 ios ipad uiview xamarin.ios

我正在尝试了解 View Controller 、 subview Controller 、 View 、容器等。我正在制作一个简单的应用程序,它的左侧会有一组图标,单击它们会打开一个右侧的不同屏幕。基本设计是:

enter image description here

菜单栏将始终位于左侧,内容位于蓝色框中。我说我应该有以下结构是否正确:

enter image description here

然后当在左侧 UIView 上按下一个图像时,我应该将一个新的 UIView 推到右侧吗?或者这些应该是 UIViewControllers 吗?容器从哪里进入它?

是否可以通过 Storyboard 来完成此操作,或者是否无法链接按钮以将新的 UIView 推送到屏幕的不同部分,因此我需要通过代码来完成?

最佳答案

一个选项是使用 UISplitViewController 运行,它会为您完成大部分工作。但这是我将如何去做的。

  1. Have a HomeViewController that is the base layer of control. This class will handle:

    1.1 The UI for the buttons on the left

    1.2 Controlling what happens when a left button is pressed

    1.3 Switching out child view controllers on the right

  2. On the left side have a UIView call it navView or something and on the right have another UIView called containerView.

    2.1 The navView will always be visible

    2.2 The containerView is responsible for displaying your childViewController's view

  3. Whenever a button on the left is pressed you remove the current childViewController and add a new one.

Here is a good tutorial on it


编辑**

回应评论:

当您初始化 TabBar 时,它会保留一个 viewController 数组。所以你的权利, viewDidLoad 只被调用一次。要实现这一点,您所要做的就是保留所有 viewController 的数组并从那里切换它们。如果你愿意,我可以发布代码,这对于最初的问题来说似乎有点矫枉过正

这里是我如何做的示例:

- (void)updateChildViewController {

    //This is the VC that was being shown, but will be replaced by the new currentlyVisisbleViewController, we keep track of it so in the transitionFromViewController: method we have viewController to switch from.
    UIViewController *previousVisibleViewController = self.currentlyVisibleViewController;

    //We keep and array of both view controllers so that we only have to load them into memory once, so their viewDidLoad is only called once.
    if (!viewControllers) {
        viewControllers = [[NSMutableArray alloc] initWithObjects:[NSNull null], [NSNull null], nil];
    }

    //If a given VC has not been loaded before we know becuase it's spot in the viewControllers array will be Null. (as seen in the line above)
    if ([viewControllers objectAtIndex:self.displayType] == [NSNull null]) {

        id newlyLoadedViewController;

        //I have two display types I switch between, a map and list
        if (self.displayType == DTMap) {
            newlyLoadedViewController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];
        }
        else if (self.displayType == DTList) {
            newlyLoadedViewController = [[ListViewController alloc] initWithNibName:@"ListViewController" bundle:nil];

        }

        //Set the object in it's location in viewControllers array
        [viewControllers setObject:newlyLoadedViewController atIndexedSubscript:self.displayType];
    }

    //Set the new currentlyVisibleViewController
    self.currentlyVisibleViewController = [viewControllers objectAtIndex:self.displayType];

    //Adjust the frame to fit in the contentView (or the "container" view)
    self.currentlyVisibleViewController.view.frame = self.contentView.frame;

    //Make sure that it resizes on rotation automatically along with the contentView.
    self.currentlyVisibleViewController.view.autoresizingMask = self.contentView.autoresizingMask;

    //Let the old VC know that is going to be removed if it exist. We lazy load the UIViewControllers so on intial launch, there is only one UIViewController loaded, once we switch to another one we will have a previousVisibleViewController.
    if (previousVisibleViewController) {
        [previousVisibleViewController willMoveToParentViewController:nil];
    }

    //Add the currentlyVisibleViewController as a childViewController
    [self addChildViewController:self.currentlyVisibleViewController];

    //If there was a previousVC then we animate between them
    if (previousVisibleViewController) {
        [self transitionFromViewController:previousVisibleViewController
                          toViewController:self.currentlyVisibleViewController
                                  duration:0.0f
                                   options:UIViewAnimationOptionTransitionNone
                                animations:^{}
                                completion:^(BOOL finished) {

                                    //Notify the new visible viewController than the move is done
                                    [self.currentlyVisibleViewController didMoveToParentViewController:self];

                                    //Tell the old one it is no longer on the screen and has been removed.
                                    [previousVisibleViewController removeFromParentViewController];
                                }];
    }

    //Otherwise it's the first time we are adding a child so we need to link the views
    else {

        //Add it to content view, calls willMoveToParentViewController for us. We only have to set this once.
        [self.contentView addSubview:self.currentlyVisibleViewController.view];
    }

}

关于ios - UIView & UIViewController 应用程序设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848114/

相关文章:

ios - iPhone 可以充当 NFC 标签吗?

ios - 使用 Apple 登录 - 不同的行为模拟器 VS iPhone

ios - 如何在应用程序委托(delegate) (obj-c) 中添加 UIAlertController

ios - 使用 iOS 8.1 在 iPad 上缩放相机时的 UIImagePickerController 问题

swift - 在运行时获取 subview 的实际宽度(Swift 4)

ios - 当表在 ScrollView 内时编辑 UITableviewCell 不起作用

iOS - 带有 Storyboard的 UISplitViewController - 多个主视图和多个详细 View

ios - 仅捕获 UIView 2 手指 UIPanGestureRecognizer

ios - initWithCoder : and initWithFrame: 之间共享初始化方法的命名约定

ios - 将其他 View 拖到 View 上时,还有另一种检测 View 的方法吗?