子类化 UIViewControllers 视图
例如,我在我的 uiviewcontroller 中有这个
- (void)loadView {
CGRect frame = [[UIScreen mainScreen] applicationFrame];
MyDetailView *detailView = [[MyDetailView alloc] initWithFrame:frame];
self.view = detailView;
[detailView release];
}
,现在我在我的视图中添加一些标签,
@property(nonatomic, retain) UILabel *descriptionLabel;
如果我想在我的 ViewController 中设置它,我通常会做类似的事情
self.view.descriptionLabel.text = @"foo";
,因为这会生成一个警告,我将其更改为
[[(MyDetailView*)self.view descriptionLabel] setText:@"foo"];
是否有更好的方法这? FE 告诉我的 ViewController 视图属性是一个子类?有没有办法做到这一点,或者我应该将我的详细视图保存在另一个 ivar 中?
感谢您的提示!
so for example i have this in my uiviewcontroller
- (void)loadView {
CGRect frame = [[UIScreen mainScreen] applicationFrame];
MyDetailView *detailView = [[MyDetailView alloc] initWithFrame:frame];
self.view = detailView;
[detailView release];
}
now i add some labels to my view, f.e.
@property(nonatomic, retain) UILabel *descriptionLabel;
if i want to set this in my ViewController i would normally do something like
self.view.descriptionLabel.text = @"foo";
because this would generate a warning i cange it to
[[(MyDetailView*)self.view descriptionLabel] setText:@"foo"];
is there a better way for this? F.E. telling my ViewController that the view attribute is a subclass? is there a way for this or should i save my detail view in an additional ivar?
thanks for your hints!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为你所展示的有什么问题。我的写法可能略有不同,但这可能是一个偏好问题:
I don't believe there's anything wrong with what you've demonstrated. I might write it slightly differently, but that's probably a matter of preference:
按照您编写的方式,看起来您正在将 UILabel 设置为 NSString 值,所以也许这只是一个编辑。你尝试过吗
The way you have it written, it looks like you're setting a UILabel to an NSString value, so maybe this is just an edit. Have you tried
如果您想以编程方式执行此操作,isaac 的答案非常好。只需将您的自定义视图设置为它自己的变量,这样您就可以在文件中的任何位置绘制信息并从中传递信息。这个答案只是添加到您的选项中的一个替代方案。如果您已在 IB 中完成此操作,最简单的方法是选择视图控制器的 View 并将默认
UIView
中的 Class 设置为您的自定义 MyDetailView。然后调用 self.view 将直接访问自定义视图的属性。isaac's answer is great if you want to do it programmatically. Just set up your custom view to be its own variable, so you can draw and pass information to and from it anywhere in the file. This answer is merely an alternative to add to your options. If you've done this in IB, the easiest way would be to select the view controller's View and set Class from the default
UIView
to your custom MyDetailView. Then callingself.view
will access the properties of your custom view directly.