子类化 UIViewControllers 视图

发布于 2024-12-08 07:28:41 字数 717 浏览 0 评论 0原文

例如,我在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

颜漓半夏 2024-12-15 07:28:41

我不认为你所展示的有什么问题。我的写法可能略有不同,但这可能是一个偏好问题:

myView = (MyDetailView *)[self view];
[myView setDescriptionLabel:@"foo"];

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:

myView = (MyDetailView *)[self view];
[myView setDescriptionLabel:@"foo"];
千秋岁 2024-12-15 07:28:41

按照您编写的方式,看起来您正在将 UILabel 设置为 NSString 值,所以也许这只是一个编辑。你尝试过吗

self.view.descriptionLabel.text = @"foo";

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

self.view.descriptionLabel.text = @"foo";
骑趴 2024-12-15 07:28:41

如果您想以编程方式执行此操作,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 calling self.view will access the properties of your custom view directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文