在 Objective-C 中使用子类化可以走多远?

发布于 2024-07-23 05:24:12 字数 197 浏览 4 评论 0原文

在 iPhone 应用程序中,我需要自定义 UINavigationController 类的外观。 例如,增大栏和字体大小。 我的客户确实需要这个,以及更大的按钮。

因此,我决定对 UINavigationController 进行子类化,而不是从头开始编写该类。 我的问题是,我可以自定义任何方法或属性的默认值吗? 子类化类时有什么限制吗?

In an iPhone app, I need to customize the look of a UINavigationController class. For instance, make the bar and font size bigger. My client really needs that, and bigger buttons aswell.

So, instead of writing the class from scratch, I decided to subclass UINavigationController. My question is, can I customize any method or attribute's default value? Is there any restriction while subclassing a class?

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

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

发布评论

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

评论(2

第几種人 2024-07-30 05:24:12

这取决于你所说的“限制”是什么意思。 运行时不会阻止您对任何您喜欢的内容进行子类化,但有一些问题。 (虽然你的答案是专门关于 UINavigationController 的,但标题和概念更大,所以我将解决更大的问题。)

  1. 父类中可能有标记为 @private 的变量,而子类不能访问,与默认的 @protected 相比,这是可访问的。
  2. 有些类是为子类化而设计的(大多数 AppKit 和 UIKit 类都是如此),但对于其他类来说,这是极其不明智的。 例如,像 NSString、NSMutableArray 等关键基础类实际上是类簇,这意味着该类的实例实际上可能是几个私有类之一。
  3. 为子类化而设计的类通常会记录必须重写的关键方法。 例如,NSDictionary 和 NSMutableDictionary 描述了一些“原始”方法,其他方法都会调用这些方法,因此在每种情况下重写 2 或 3 个方法就足以更改行为,而无需重新实现其他所有内容。
  4. 如果有必要,请极其小心地调用父类对重写方法的实现。 这在 UI 类中尤其重要,因为 UI 类通常具有比简单的获取/设置数据类更复杂的行为。

在这种情况下,您不从头开始编写类是完全正确的 - 这将是一个坏主意™。 如果您想更改属性(字段?)的默认值,您可以在调用父级初始化程序之后在初始化程序中设置所需的值。 但是,如果您这样做,请确保您没有在父类中存放某些内容,并且始终进行彻底的测试。

Apple 保留了以“_”开头的方法供自己使用,因此我同意 Marc 的警告,不要碰这些方法。 当然,其他一切都是公平的。 :-)

It depends what you mean by "restriction". The runtime will not prevent you from subclassing whatever you like, but there are a few gotchas. (Although your answer is specifically about UINavigationController, the title and concept are bigger, so I'll address the bigger issues.)

  1. There may be variables in the parent class which are marked @private and child classes cannot access, versus the default of @protected, which are accessible.
  2. Some classes are designed for subclassing (and most AppKit and UIKit classes are) but for others it is extremely unwise. For example, key foundation classes like NSString, NSMutableArray, etc. are actually class clusters, meaning an instance of the class may actually be one of several private classes.
  3. Classes that are designed for subclassing will usually document the key methods which must be overridden. For example, NSDictionary and NSMutableDictionary describe a few "primitive" methods which every other method calls down to, so overriding 2 or 3 in each case is sufficient to change the behavior without re-implementing everything else.
  4. Be extremely careful to call the parent class' implementation of your overridden method if and when it is necessary. This can be especially important in UI classes, which often have more complex behavior than simple get/set data classes.

In this case, you're exactly right to not write the class from scratch — that would be a Bad Idea™. If you want to change the default value of an attribute (field?), you can set the desired value in an initializer after calling the parent's initializer. However, if you do this, make sure you're not hosing something in the parent class, and always test thoroughly.

Apple has reserved methods that start with "_" for their own use, so I'd echo Marc's caution to not touch those at all. Everything else is fair game, within reason of course. :-)

逆夏时光 2024-07-30 05:24:12

不,随意发疯。 许多 Cocoa 类不像其他类那样子类化友好(尤其是那些更多地依赖委托控制的类),并且可能不会公开您需要的功能,但您可以随意子类化任何非私有方法(以 _ 开头)字首)。 如果您确实愿意,您甚至可以对它们进行子类化,尽管在 iPhone 上 Apple 很可能会拒绝您的应用程序。

No, feel free to go nuts. Many Cocoa classes are not as subclassing-friendly as others (especially those that are meant to rely more on delegate control) and may not expose the functionality you need, but you can feel free to subclass any method that's not private (begins with a _ prefix). You could even subclass those if you really wanted to, although on the iPhone chances are Apple would reject your app.

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