简体   繁体   English

情节提要中View Controller外部的UIView

[英]UIView outside of a View Controller in a Storyboard

I'm stuck with a question I can't find an answer to in the official documentation, on Stackoverflow or on Google. 我遇到一个问题,在Stackoverflow或Google的官方文档中找不到答案。

I'm an experienced iOS developer but new to Storyboards. 我是一位经验丰富的iOS开发人员,但还是Storyboards的新手。 I'd like to instantiate a view on the current viewcontroller, a view that, in a NIB, I would draw outside the viewController and attach an outlet to to refer to it in code and call something like addSubview: or so. 我想实例化当前viewcontroller上的视图,该视图在NIB中会在viewController外部绘制并附加一个出口以在代码中引用它,并调用诸如addSubview:之类的东西。 It's useful to draw an overlay or, in my current case, a callout view for use in a MapView delegate. 绘制覆盖图或在我当前的情况下,在MapView委托中使用的标注视图非常有用。 But I can't figure out how I can do this in a Storyboard. 但是我不知道如何在情节提要中做到这一点。

I can't seem to have a view outside of a viewcontroller, and views inside the viewcontroller either have to be subviews of the controller's view or don't be visible in Interface Builder at all (which is okay when you drag a view composed outside, in a traditional NIB, but quite a problem if you need to tweak even the least detail of it). 我似乎看不到viewcontroller之外的视图,并且viewcontroller内的视图要么必须是控制器视图的子视图,要么根本就不在Interface Builder中可见(当您将视图拖到外部时就可以了) ,这在传统的NIB中是非常困难的,如果您需要对其进行最细微的调整,那将是一个很大的问题)。

Any hint on what to do or where to look? 关于做什么或在哪里看的任何提示? Thanks. 谢谢。

It appears there is no way to do it. 看来没有办法。 See Jason's comment and this question addressing the same problem, though in a different context. 杰森的评论这个问题的解决同样的问题,虽然在不同的上下文。

However, I realised earlier when thinking about the implications of my problem — as I began to explain in my previous comment before deciding it would be worth a full answer — it makes sense to separate the design of a custom view from the design of an application flow. 但是,当我考虑问题的含义时,我早些意识到了(正如我在上一个评论中开始解释之前,认为它值得一个完整的答案),将自定义视图的设计与应用程序的设计分开是有意义的。流。 So the fact that it's not possible to design custom views in Storyboards may very well be... by design. 因此,无法在情节提要中设计自定义视图的事实很可能是……。

A custom view (which is a reusable component) is very different from a view controller (which represents a scene in your application storyboard). 自定义视图(是可重用的组件)与视图控制器(代表应用程序情节提要中的场景)有很大不同。 It's the same as a character and a scene, in a movie. 它与电影中的角色和场景相同。 You don't want to design your character in your scene, even if you happen to use that character in only one scene. 即使您只是在一个场景中使用该角色,也不想在场景中设计角色。 It's fortuitous if you use it in only one scene, but not essential. 如果仅在一个场景中使用它是偶然的,但不是必需的。 So it has to be separate. 因此,它必须分开。

The fact that you want to use Interface Builder to design your custom view is only an implementation detail that should be encapsulated in your class. 您想要使用Interface Builder设计您的自定义视图的事实只是应该封装在您的类中的实现细节 The application storyboard has no need of knowing, even less managing, how you deal with the particular implementation of your reusable view. 应用程序情节提要不需要知道,甚至不需要管理,就如何处理可重用视图的特定实现。

Hmm, I wish I had more of this brand of interrogation :) 嗯,我希望我能更多地接受这种审问:)

You can use a Container View, is like a regular view(You can assign a IBOutlet UIView) but you can define it outside of your UIViewController. 您可以使用容器视图,就像常规视图一样(可以分配IBOutlet UIView),但是可以在UIViewController之外定义它。

“容器视图”在视图外部,但是我可以将其分配给IBOutlet UIView你可以设计

I was wondering the same thing myself, and actually was able to get this to happen, though after messing around, its gone. 我自己也想知道同样的事情,并且实际上能够做到这一点,尽管在弄乱之后,它已经消失了。 I have a Nav Controller, a View Controller and two views. 我有一个导航控制器,一个视图控制器和两个视图。 I had developed the views in a separate nib, and imported them to this project. 我在单独的笔尖中开发了视图,并将其导入到该项目中。 While was was connecting buttons, the view I was working in suddenly appeared next to my View Controller. 当正在连接按钮时,我正在使用的视图突然出现在我的View Controller旁边。 Unfortunately, while trying to get the other view to appear, I somehow lost it, and dont know how to get it back. 不幸的是,当试图使另一个视图出现时,我莫名其妙地丢失了它,并且不知道如何恢复它。 It doesnt seem like you can drag a view to the storyboard, only into a view controller. 似乎您不能将视图拖到情节提要中,而只能拖到视图控制器中。 However, if I was able to do it once, its got to be reproduceable. 但是,如果我能够做一次,那么它必须是可复制的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM