xcode 有没有办法在故事板中“设计”自定义 UIView,并在多个 ViewController 中使用而不添加到任何特定控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20370366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to 'design' a custom UIView in storyboard, and use in multiple ViewControllers without adding to any specific controller?
提问by Sti
I'm in need of a custom UIView
which is to be used in multiple different ViewControllers
.
Usually when I create a custom UIView
, I drag an instance of UIView
into my ViewController
in storyboard, create a subclass of UIView
, and point to this subclass in the Identity Inspector
for the view. From there, I would connect the UI-objects as outlets to the header-file of the subclass.
我需要一个UIView
用于多个不同ViewControllers
. 通常当我创建一个 custom 时UIView
,我将一个 的实例拖到UIView
我ViewController
的故事板中,创建一个 的子类UIView
,并Identity Inspector
在视图中指向这个子类。从那里,我将 UI 对象作为出口连接到子类的头文件。
The view I want to make now, is not supposed to be a part of any specific controller, but should be added to any controller that asks for it. A rogue UIView
. I know I can create the entire UIView
programmatically, and just create an instance of it, but I'd like to have it (and design it) in my storyboard.
我现在想要创建的视图不应该是任何特定控制器的一部分,而应该添加到任何需要它的控制器中。一个流氓UIView
。我知道我可以以UIView
编程方式创建整个,并且只创建它的一个实例,但我想在我的故事板中拥有它(并设计它)。
In Storyboard, the only objects I'm allowed to drag 'outside a ViewController', are other ViewControllers.
在 Storyboard 中,我被允许拖到“ViewController 之外”的唯一对象是其他 ViewController。
I have never used anything other than Storyboard for iOS-developing, but I have come over tutorials which have been using the other mode, from the olden days, which looks like what I need. Is it possible to get something similar intomy storyboard? Or would this require its own setup/design? If so, how?
我从来没有使用比故事板为iOS,开发其他任何事情,但我已经使用其他模式教程,过来从昔日,它看起来像我需要什么。是否有可能得到类似的东西进入我的故事板?或者这是否需要自己的设置/设计?如果是这样,如何?
I was also thinking of solving this with adding a 'phantom' UIViewController
containing my custom View; designing it there, but instantiate it from the other controllers, but this sounds so hacky..
我也在考虑通过添加一个UIViewController
包含我的自定义视图的“幻影”来解决这个问题;在那里设计它,但从其他控制器实例化它,但这听起来很hacky..
I'd like to do this with a UITableViewCell
as well, but I guess that would have the same solution.
我也想用 a 来做这个UITableViewCell
,但我想那会有相同的解决方案。
回答by Chris
For your UIView, you should be creating a custom UIViewController in your storyboard and instantiate the view controller to access the view:
对于你的 UIView,你应该在你的故事板中创建一个自定义的 UIViewController 并实例化视图控制器以访问视图:
MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
// Access the view using "myViewController.view"
This is a very common practice in iOS since storyboards were presented. If you are using multiple storyboards, you should create a new instance of UIStoryBoard with:
这是 iOS 中非常常见的做法,因为故事板出现了。如果您使用多个故事板,您应该使用以下命令创建 UIStoryBoard 的新实例:
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
And then instantiate the view controller with this instance of the storyboard.
然后用故事板的这个实例实例化视图控制器。
If you want to solely use a UIView, you should be creating a .xib file, i.e. the olden daysformat. To create a custom UITableViewCell, I would absolutely use a .xib file. Your last option would be to create a UIView programmatically, which could be called from any place in your application. Depending on the complexity of this view, this may be a valid option.
如果你想单独使用一个 UIView,你应该创建一个 .xib 文件,即过去的格式。要创建自定义 UITableViewCell,我绝对会使用 .xib 文件。您的最后一个选择是以编程方式创建一个 UIView,它可以从您的应用程序中的任何位置调用。根据此视图的复杂性,这可能是一个有效的选项。
回答by Eric Qian
I don't think you can create a custom UIView in storyboard. So in this case, you can create a xib file for that custom view. Then when you want use that view, just use
我认为您无法在故事板中创建自定义 UIView。因此,在这种情况下,您可以为该自定义视图创建一个 xib 文件。然后当你想使用该视图时,只需使用
UIView *customView = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil] objectAtIndex:0];
to get that view.
得到那个观点。