ios 从 xib 或其他场景添加子视图与故事板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8754157/
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
Add subview from a xib or another scene with storyboard
提问by shiami
I'm new to iOS and Xcode. I can't figure out how to design a separated view and make it be added into the main UIViewController using storyboard.
我是 iOS 和 Xcode 的新手。我不知道如何设计一个单独的视图并使用故事板将其添加到主 UIViewController 中。
I did different approaches..
我做了不同的方法..
- Just grab an UI object from right-bottom corner window in the xcode, and then put it onto any space area of storyboard. But I can't drop the UI object like the way with xib.
- Add a new UIViewController. Add a view into the
UIViewController
. In the main ViewController.m, I get the newUIViewController
instance in the viewDidLoad, and then[self.view addSubview:newUIViewController.view]
. But I can't see the added view. - I created a new xib file. And add a view into it. I also try to get the instance in the main
ViewController
. AndaddSubview
with the xib's view. But it also failed.
- 只需从 xcode 的右下角窗口中抓取一个 UI 对象,然后将其放到故事板的任何空间区域。但是我不能像使用 xib 那样删除 UI 对象。
- 添加一个新的 UIViewController。将视图添加到
UIViewController
. 在主 ViewController.m 中,我UIViewController
在 viewDidLoad 中获取新实例,然后[self.view addSubview:newUIViewController.view]
. 但是我看不到添加的视图。 - 我创建了一个新的 xib 文件。并在其中添加一个视图。我也尝试在 main 中获取实例
ViewController
。并addSubview
与 xib 的观点。但它也失败了。
Is there a correct way or any working solution to do so?
是否有正确的方法或任何可行的解决方案?
回答by shiami
I figured out a way to do it. Described as following:
我想出了一种方法来做到这一点。描述如下:
- Create a .xib file. For example: MyView.xib
- Create a objective-c class. For example: MyViewClass.h and MyViewClass.m
- Set the .xib File's Owner to the class.
- Add a UIView element on the storyboard and set the custom class to the objective-c class name (MyViewClass).
The key-point is to override the initWithCodermethod in the object-c class.
- (id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0]]; } return self; }
- 创建一个 .xib 文件。例如:MyView.xib
- 创建一个objective-c 类。例如:MyViewClass.h 和 MyViewClass.m
- 将 .xib 文件的所有者设置为类。
- 在storyboard上添加一个UIView元素,并将自定义类设置为objective-c类名(MyViewClass)。
关键点是覆盖object-c 类中的initWithCoder方法。
- (id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] objectAtIndex:0]]; } return self; }
The idea is the custom class is loaded by the storyboard and initWithCode will be called. The index 0 is the root view in the .xib interface builder.
这个想法是自定义类由故事板加载,initWithCode 将被调用。索引 0 是 .xib 界面构建器中的根视图。
It's kind of tricky but it works.
这有点棘手,但它有效。
回答by T.J.
In storyboard, drag and drop a view controller. The view controller come with a main view. Select that main view by clicking outside of the added view controller and then clicking in the center of it. Now, just drag a uiview or whatever into that main view just like you did with IB.
在故事板中,拖放一个视图控制器。视图控制器带有一个主视图。通过单击添加的视图控制器外部然后单击它的中心来选择该主视图。现在,只需将 uiview 或其他任何内容拖到主视图中,就像您对 IB 所做的一样。