Storyboard - 在 Storyboard 中为同一个 ViewController 创建多个视图
我正在使用故事板设计 iPad 应用程序。我有一个显示视图的ViewController
。我的问题是,当管理员看到该屏幕时,他应该看到某个视图,而当用户看到该屏幕时,他应该看到另一个视图。我认为我应该为同一屏幕创建两个视图,并根据登录者加载适当的视图。但是,虽然我可以在之前的 iOS 版本中的 XIB 文件中执行此操作,但 Storyboard 不允许我在 ViewController 之外创建视图,因此我无法设计多个视图。
我的问题是:
- 我们可以在故事板中为同一个 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.
My questions are :
- Can we design two views in storyboard for the same
ViewController
? If yes, then how? - 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.
Thanks in advance for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在另一个故事板控制器中显示一个故事板控制器的视图,但这很棘手。我在故事板中有一个选项卡视图控制器,其中一个视图有一个导航栏,上面有一个分段控件,用于确定两个视图中的哪个视图出现在控制器中。我将其称为“主”控制器。我所做的是在主视图内的“主”控制器上,我在导航栏和选项卡栏之间添加了两个彼此重叠的视图,并将 IBOutlet 连接到它们。
当分段控件的左段被按下时,rightView被隐藏(setHidden:TRUE)并且leftView被取消隐藏。对于正确的细分市场,反之亦然。
为了显示上述视图之一内另一个 ViewController 的视图,在“主”视图控制器中,我为每个辅助 ViewController 创建了一个 IBOutlet
辅助视图控制器的布局必须在外观方面与“主”控制器相匹配 化导航栏、状态栏和选项卡栏项目
然后,我必须在“主”视图控制器上的 ViewDidLoad 中手动从情节提要中实例
。其中“CustomViewControllerOne”和“CustomViewControllerTwo”是我必须输入的故事板中控制器的“标识符”字段值。
再次在“主”控制器上的 ViewDidLoad 中,我将控制器视图添加为我基于段控件隐藏和取消隐藏的视图的子视图
,我发现如果我尝试将它们添加为主视图的子视图而不创建两个视图容器(leftView 和 rightView)辅助视图控制器在“主”控制器中出现偏移。
因此,当用户按下左侧部分时,会出现 CustomViewController1 中的视图,而当用户按下右侧部分时,会出现 CustomViewController2 中的视图。
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.
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
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.
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
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.