gpt4 book ai didi

ios - 不加载带有 Nib (.xib) 的自定义 UIView 的 subview ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:45:17 26 4
gpt4 key购买 nike

我正在尝试使用关联的 Nib (.xib) 文件创建自定义 UIView 类,以帮助进行布局和创建。

现在,我的自定义 View 中没有 subview 出现,即使它们存在于我的 nib 文件中并通过 IBOutlets 正确连接到我的自定义 View 类。

我尝试通过以下其他答案: Load custom UIView with XIB from a View Controller's view using IB

还有这个: How do I get a view in Interface Builder to load a custom view in another nib?

还有这个: http://soulwithmobiletechnology.blogspot.com/2011/06/create-uiview-with-nib-file-in-xcode-4.html

但是方法[[NSBundle mainBundle] loadNibNamed:@"MyView"owner:self options:nil] 似乎调用了-[initWithCoder:],导致无限循环。

请让我知道要包含哪些其他详细信息以帮助解决此错误!

更新 1:我是否需要通过 -[layoutSubviews] 布置我的 subview ?我的 nib 文件实际上没有布局吗?如果是这样,那么 nib 文件的意义何在?

最佳答案

打开您的任何 View Controller 并将此代码粘贴到您的 viewDidAppear 方法中:

NSArray * allTheViewsInMyNIB = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil]; // loading nib (might contain more than one view)
MyCustomView* problemView = allTheViewsInMyNIB[0]; // getting desired view
problemView.frame = CGRectMake(0,0,100,100); //setting the frame
[self.view addSubview:problemView]; // showing it to user

这就是您所要做的,以呈现一个 View 。如果你想与这个 View 交互——你必须在类的接口(interface)中声明 MyCustomView* problemView,而不是在方法中。

关于ios - 不加载带有 Nib (.xib) 的自定义 UIView 的 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19137846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com