iPhone - 行为类似于 UIViewController 的 UIView

发布于 2024-12-12 05:58:58 字数 255 浏览 0 评论 0原文

我需要创建一个类来呈现 UIVIew 并有一些代码在准备显示之前对其进行初始化。我需要知道视图何时准备好,我的意思是,我需要像 viewDidLoad 或 viewWillAppear 这样的东西,但由于它是 UIVIew 它缺少这些协议。

我无法将其实现为 UIViewController,因为我不想以模式方式呈现它。它实际上是一个需要在屏幕一侧显示的矩形视图。

我如何声明这个类?如果该类是基于 UIView 的,我就没有我提到的方法。

谢谢

I need to create a class that will present a UIVIew and has a some code to initialize it before it is ready to show. I need to know when the view is ready, I mean, I need something like viewDidLoad or viewWillAppear, but as it is a UIVIew it lacks these protocols.

I cannot implement it as a UIViewController as I don't want to present it modal. It is really a rectangular view that needs to show on a screen side.

How do I declare this class? If the class is a UIView based I don't have the methods I mentioned.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

神妖 2024-12-19 05:58:58

有什么理由不在 UIViewinitWithFrame 方法中执行此类操作?此外,您还可以在 layoutSubviews 上执行其他操作。视图控制器有 viewDidLoad 因为视图是延迟加载的(从笔尖或其他方式 - 它也有一个 loadView)。它有 viewWillAppearviewWillDisappear 因为它管理视图(顺便说一句,即使视图控制器也由另一个视图控制器管理 - 这些方法被称为当你在 UINavigationController 或 UITabBarController 或管理 UIViewControllers 的类中拥有控制器时 - 视图本身并不真正管理任何东西,为此,你有。 。

在视图加载时执行一些繁重的操作肯定会降低 UI 性能。您可能需要实现另一种设计模式来异步分配数据值 到您的自定义视图的实例 - 完成后,您可以调用 layoutSubviewssetNeedsDisplay 来更新视图。

Any reason to not do that kind of stuff inside the initWithFrame method on a UIView? Also, you can do additional stuff on layoutSubviews. A view controller has viewDidLoad because the view is lazy loaded (from a nib or otherwise - it also has a loadView). It has viewWillAppear and viewWillDisappear because it is managing the view (btw, even the view controller is managed by another view controller - these methods are called when you have the controller within a UINavigationController or UITabBarController or such classes which mange UIViewControllers. - the view itself is not really managing anything. All it knows about is how to draw itself. For that, you have layoutSubViews, drawRect, etc.

Doing some heavy stuff upon view's load will definitely kill the UI performance. You probably need to implement another kind of design pattern that will asynchrounously assign data values to the instance of your custom view - when that is done, you call layoutSubviews or setNeedsDisplay to update the view.

网白 2024-12-19 05:58:58

您描述的场景不是不实现 UIViewController 的理由。假设你有一个容器视图 A 和一个子视图 B。两者都有自己的 UIViewController AC 和 BC。现在在 AC 上添加由 BC 管理的视图 B:

[self.view addSubview:BC.view];

The scenario you described is no reason for not implementing a UIViewController. Assume you have a container view A and a subview B. Both have their own UIViewController AC and BC. Now on AC you add the View B managed by BC:

[self.view addSubview:BC.view];
许你一世情深 2024-12-19 05:58:58

您可能想使用UIViewController

您不必只是以模态方式呈现它们。您可以使用 yourViewController.view 获取视图控制器的视图,并将其添加为您想要的任何视图的子视图。

如果您的目标是 iOS 5,则有一些新方法(例如 addChildViewController:)旨在使此类操作变得更容易。不过,您也可以在 iOS 4 上执行此操作,并且仍然有效。

You probably want to be using UIViewController.

You don't just have to present them modally. You can get your view controller's view with yourViewController.view, and add that as a subview of whatever view you want.

If you're targeting iOS 5, there are a few new methods (such as addChildViewController:) designed to make doing things like this easier. You can do it on iOS 4 too though, and it'll still work.

短暂陪伴 2024-12-19 05:58:58

在你的UIView中实现一个drawRect,加上一个初始化标志。在显示视图之前,将调用drawRect。如果尚未设置初始化标志,请进行初始化并设置标志。仅当您的初始化可以快速完成(没有长时间的同步调用)时,这才会看起来不错。

Implement a drawRect in youR UIView, plus an initialization flag. Just before the view is to be displayed the drawRect will be called. If the initialization flag isn't yet set, do your initialization and set the flag. This will only look good if your initialization can be done quickly (no long synchronous calls).

夕色琉璃 2024-12-19 05:58:58

我需要创建一个类来呈现 UIVIew,并有一些代码在准备显示之前对其进行初始化。我需要知道视图何时准备好,我的意思是,我需要像 viewDidLoad 或 viewWillAppear 这样的东西,但由于它是 UIVIew 它缺少这些协议。

您可能需要重新考虑如何使用您的视图。听起来您试图将太多类似控制器的逻辑放入您的视图中。这就是为什么你希望你的视图表现得像一个控制器。

更具体地说:您到底想实现什么目标?如果您在显示视图之前等待数据加载,那么实际上可能需要将某些内容放入调用视图的控制器中。

为了说明我的观点,假设您正在将一些文本放入从磁盘读取的 UILabel 中。从磁盘读取数据与视图并不真正相关。视图只关心它显示什么文本,而不关心它如何接收文本。从磁盘读取后,您可以使用您读取的文本创建一个 UILabel。这使得 UILabel 更加灵活。

该示例可能与您正在做的事情完全无关,但我用它作为视图和控制器之间差异的示例。任何与视图的显示和绘制无关的内容都不应该属于那里。

I need to create a class that will present a UIVIew and has a some code to initialize it before it is ready to show. I need to know when the view is ready, I mean, I need something like viewDidLoad or viewWillAppear, but as it is a UIVIew it lacks these protocols.

You might want to rethink how your view is being used. It sounds like you're trying to put too much controller-like logic into your view. That's why you're wanting your view to behave like a controller.

More specifically: What exactly are you trying to accomplish? If you're waiting for data to load before display the view, that might actually be something to put in the controller that is calling the view.

To illustrate my point, imagine you're putting some text in a UILabel that you read from disk. The reading from disk isn't really related to the view. The view only cares what text it displays, not how it received the text. Once it's read from disk, you can create a UILabel with that text that you read. This allows the UILabel to be more flexible.

That example might not be at all related to what you're doing, but I use it as an example of the difference between a view and a controller. Anything not related with the display and drawing of the view shouldn't belong there.

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