故事板 - 在同一个ViewController的storyboard中创建多个视图 [英] Storyboard - Creating Multiple Views in storyboard for the same ViewController

查看:101
本文介绍了故事板 - 在同一个ViewController的storyboard中创建多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用故事板设计iPad应用程序。我有一个显示视图的 ViewController 。我的问题是,当管理员看到该屏幕时,他应该看到某个视图,而当用户看到该屏幕时,他应该看到另一个视图。我以为我应该为同一个屏幕创建两个视图,并根据谁记录它来加载适当的视图。但是,虽然我可以在之前的iOS版本中的 XIB 文件中执行此操作,但故事板不允许我在 ViewController 所以我无法设计多个视图。

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 certain view whereas when a user sees that screen, he should see another view. I thought I should create two views for the same screen and load the appropriate one depending on who has logged it. However, while I could do this in XIB files in prior iOS versions, the storyboard does not allow me to create a view outside the ViewController so i cannot design multiple views.

我的问题是:


  1. 我们可以在故事板中设计两个视图相同 ViewController ?如果是,那怎么样?

  2. 我需要并排看到两个视图,所以我可以单独对它们进行更改。将它们作为主视图的子视图将在运行时加载两个视图。我希望能够只加载其中一个视图,具体取决于谁登录。

  1. Can we design two views in storyboard for the same ViewController? If yes, then how?
  2. I need to see the two Views side by side so I can make changes to them separately. Having them as sub-views of the main view will load both the views at run-time. I want to be able to load only one of the views depending on who has logged in.

提前感谢您的帮助

推荐答案

您可以在另一个中显示一个故事板控制器的视图,但这很棘手。我在故事板中有一个标签视图控制器,其中一个视图有一个带有分段控件的导航栏,它确定控制器中出现了两个视图的哪个视图。我将其称为主要控制器。我所做的是在主要控制器上,在主视图中,我在导航栏和标签栏之间添加了另外两个视图,并将IBOutlet连接到它们。

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;

当按下分段控件的左段时,隐藏了rightView(setHidden:TRUE)并且leftView被取消隐藏。对于正确的段,反之亦然。

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.

要在上面的一个视图中显示来自另一个ViewController的视图,在主视图控制器中,我为每个创建了一个IBOutlet辅助ViewController

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

然后,我必须在主视图控制器上的ViewDidLoad中手动实例化故事板。

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"];

其中CustomViewControllerOne和CustomViewControllerTwo是故事板中控制器的标识符字段值,我必须输入。

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

再次在主控制器上的ViewDidLoad中,我将Controller视图添加为我隐藏和取消隐藏的视图的子视图

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

我发现如果我尝试将它们添加为主视图的子视图而不创建两个视图容器(leftView和rightView)辅助视图控制器在主控制器中出现偏移。

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.

因此当用户按下左段时,CustomViewController1的视图出现,当它们出现时按下右侧段,CustomViewController2中的视图出现。

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.

这篇关于故事板 - 在同一个ViewController的storyboard中创建多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆