Storyboard - Creating Multiple Views in storyboard for the same ViewController

后端 未结 1 577
长情又很酷
长情又很酷 2021-02-13 10:03

I am designing an iPad application using storyboard. I have a ViewController showing a view. My problem is, that when an admin sees that screen, he should see a cer

1条回答
  •  说谎
    说谎 (楼主)
    2021-02-13 10:57

    You can show the view of one storyboard controller in another, but it's tricky. I had a tab view controller in storyboard where one of the views had a NavBar with a segmented control on it that determined which view of two views appeared in the controller. I'll call this the "primary" controller. What I did was on the "primary" controller, inside the main View, I added two more views on top of each other that fit between the navbar and the tabbar and connected IBOutlets to them.

    @property (retain, nonatomic) IBOutlet UIView *leftView;
    @property (retain, nonatomic) IBOutlet UIView *rightView;
    

    When the left segment of the segmented control was pressed, the rightView was hidden (setHidden:TRUE) and the leftView was unhidden. Vice-versa for the right segment.

    To show the view from another ViewController inside one of the above Views, in the "primary" view controller I created an IBOutlet for each secondary ViewController

    @property (retain, nonatomic) IBOutlet CustomViewController1 *leftViewController;
    @property (retain, nonatomic) IBOutlet CustomViewController2 *rightViewController;
    

    The layouts of the secondary view controllers have to match the "primary" controller in terms of the appearance of navbar, statusbar, and tabbar items

    I then had to instantiate them from the storyboard manually in ViewDidLoad on the "primary" view controller.

    self.leftViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"CustomViewControllerOne"];
    self.rightViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"CustomViewControllerTwo"];
    

    Where "CustomViewControllerOne" and "CustomViewControllerTwo" are the "identifier" field values of the controllers in storyboard, which I had to enter.

    Again in ViewDidLoad on the "primary" controller I added the Controller views as subViews of the ones I was hiding and unhiding based on the segment control

    [self.leftView addSubview:leftViewController.view];
    [self.rightView addSubview:rightViewController.view];
    

    I found that if I tried to add them as subviews of the main View without creating the two view containers (leftView and rightView) the secondary view controllers appeared offset in the "primary" controller.

    So when the user pressed the left segment, the view from CustomViewController1 appeared and when they pressed the right segment the view from CustomViewController2 appeared.

    0 讨论(0)
提交回复
热议问题