以编程方式创建自定义 UIView 作为 tableHeaderView
我想在基于导航的应用程序中创建一个类似于地址簿应用程序的“详细视图”。
我想创建一个 UIView
,它有一个 UIImageView
和一个 UILabel
,我可以将其传递给 UITableVIew
的由导航控制器推送时的 tableHeaderView 属性。标签文本和图像在加载时必须从托管对象上下文中获取信息。
我开始尝试使用 IB 来执行此操作,但是当标签文本无法更新时,无论我将 myView.UILabel.text = @"some new text" 放在哪里,我都会感到厌倦。它只是一直显示我在 IB 检查器窗口中输入的文本。
因此,如果不使用 IB,我将如何创建一个包含 UILabel
和 UIImageView
的 UIView
呢?
我应该创建一个新类作为 UIViewController 的子类还是只是 UIView 的子类?
如何为标签和图像创建子视图?
文本和图像 URL 应该在代码中的哪里设置? (viewDidLoad 或 loadView 或 viewWillLoad)(UIView,UIViewController,detailViewController)?
I want to create a 'detail view' in my navigation-based app similar to the address book app.
I want to create a UIView
that has an UIImageView
and a UILabel
that I can pass to a UITableVIew
's tableHeaderView property when pushed by a navigation controller. The label text and image must take information from the managed object context when it loads.
I started trying to do this using IB, but go fed up when the label text wouldn't update, regardless of where I put the myView.UILabel.text = @"some new text"
. It just kept presenting the text I entered in the IB inspector window.
So without using IB, how would I go about creating a UIView
that has a UILabel
and a UIImageView
in it?
Should I be creating a new class that is a sub-class of UIViewController
or just UIView
?
How do I go about creating the sub-views for the label and image?
Where should the text and image URL be set in code? (viewDidLoad or loadView or viewWillLoad)(UIView, UIViewController, detailViewController)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您开始使用 IB 并且 UILabel 文本不会更新,听起来您的视图控制器的 IBOutlet 未正确连接到 IB 中的标签。
希望这能解决您使用 IB 的问题。在 viewController 代码中,您应该更新 viewDidLoad 中的文本。
但如果你想以编程方式执行此操作,我将创建 UIView 的子类(而不是 UIViewController)。
我会覆盖 initWithFrame 并在那里完成所有设置。
如下所示:
myView.m
确保在 dealloc 方法中正确清理内存,当然这取决于您是否喜欢 make 类属性还是类变量。
If you started using IB and the UILabel text wouldn't update, sounds like the IBOutlet of your view controller isn't correctly connected to the label in IB.
Hopefully that will fix your problem with using IB. And in the viewController code, you should update that text in viewDidLoad.
But if you want to do this programmatically, I would make a subclass of UIView (not UIViewController).
And I would override initWithFrame and do all the setup there.
Something like the following:
myView.m
Make sure to clean up memory properly in the dealloc method of course depending on whether you like make class properties vs class variables.