[英]add a custom uiview with xib in my storyboard
I am using swift2.0 and I have just added a custom UIView
called SortableTableTitleView: 我正在使用swift2.0,我刚刚添加了一个名为SortableTableTitleView的自定义
UIView
:
and then I drag a UIView
in my storyboard, and I changed the class of the view to SortableTableTitleView 然后将
UIView
拖到情节UIView
中,然后将视图的类更改为SortableTableTitleView
When I tried to set the text of the label in the custom view, I just got a bad access error. 当我尝试在自定义视图中设置标签的文本时,遇到了一个错误的访问错误。
I tried to write the initwithcoder
method of the view, but I don't know how I can load the element from xib. 我试图编写视图的
initwithcoder
方法,但是我不知道如何从xib加载元素。 (It is somehow different with ObjectiveC). (与ObjectiveC有所不同)。 So, how can I solve this?
那么,我该如何解决呢?
You cannot use xib
files within the Storboard
file. 不能使用
xib
的内部文件Storboard
文件。 So either create the view fully in the Storyboard
and use it as per normal. 因此,要么完全在
Storyboard
创建视图,然后照常使用它即可。 Or, add the view from your xib
file add a subView
to the view on the storyboard
via code. 或者,从
xib
文件中添加视图,然后通过代码将一个subView
添加到storyboard
subView
上的视图。
So something like this in the viewController
: 所以在
viewController
中是viewController
:
UINib *nibFile = [UINib nibWithNibName:@"SortableTableTitleView" bundle:nil];
NSArray *views = [nibFile instantiateWithOwner:self options:nil];
UIView *sortableTableTitleView = views[0]; //Assuming it is the only view in the xib file
//Add it where you want as a normal view now:
[self.view addSubview: sortableTableTitleView];
I realise the code above is in objective-c - I'll get the Swift comparison when I get a gap (Being new to Swift and all) 我意识到上面的代码在Objective-C中-当我得到差距时我会得到Swift的比较(对Swift和其他都是新手)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.