簡體   English   中英

UIView&UIViewController App設計

[英]UIView & UIViewController App design

我試圖繞過視圖控制器,子視圖控制器,視圖,容器等。我正在制作一個簡單的應用程序,該應用程序的左下方會有一組圖標,單擊該圖標將在屏幕上打開另一個屏幕。右邊。 基本設計是:

在此處輸入圖片說明

菜單欄將始終位於左側,其內容位於藍色框中。 我是否正確地說我應該具有以下結構:

在此處輸入圖片說明

然后,當在左側的UIView上按下圖像時,我應該將一個新的UIView推到右側? 還是應該是UIViewControllers? 容器從哪里進入?

是否可以通過情節提要板來執行此操作,還是無法鏈接按鈕以將新的UIView推送到屏幕的不同部分,因此我需要通過代碼來完成?

一種選擇是與UISplitViewController一起運行,這將為您完成大部分任務。 但是,這就是我要做的事情。

  1. 有一個HomeViewController是控件的基礎層。 該類將處理:

    1.1左側按鈕的UI

    1.2控制按下左按鈕時發生的情況

    1.3切換右側的子視圖控制器

  2. 左側有一個稱為navView之類的UIView ,右側有另一個名為containerView的UIView

    2.1 navView將始終可見

    2.2 containerView負責顯示childViewController的視圖

  3. 每當按下左側按鈕時,您都將刪除當前的childViewController並添加一個新的childViewController。

這是一個很好的教程


編輯**

回應評論:

When you initialize the TabBar it keeps an array of the viewControllers. So your right, viewDidLoad is only called once. All you have to do to achieve that is keep an array of all your viewControllers and switch them from there. I can post code on this if you want, it seemed a bit overkill for the initial question

以下是我的操作方法示例:

- (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];
    }

}

我建議使用容器視圖控制器作為布局的根,然后為左側菜單使用子視圖控制器,並為當前選擇的項目/選項卡使用另一個視圖控制器。 然后,一旦選擇,您只需在右側交換(子)視圖控制器(即從父視圖控制器中刪除當前的子控制器,然后添加一個新的)。 盡管一開始聽起來可能有些復雜,但是當您繼續進行項目時,它更健壯,更容易實現更復雜的場景。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM