简体   繁体   English

在 iPhone UIImageView 中,用什么方法来覆盖实例变量 init?

[英]In iPhone UIImageView, what method to override for instance variable init?

I have a very simple app that processes touches on a UIImageView derived view.我有一个非常简单的应用程序,可以处理 UIImageView 派生视图的触摸。

The view has an array of rectangle coordinates that act as buttons.该视图有一个矩形坐标数组,用作按钮。

Since this is a simple app I handle all the touch events right in my UIImageView derived class.由于这是一个简单的应用程序,我在我的 UIImageView 派生的 class 中处理所有触摸事件。

I need to init the array of image coordinates which is an instance variable.我需要初始化作为实例变量的图像坐标数组。

I tried overriding several methods including init, but none of the ones I tried are called.我尝试覆盖包括 init 在内的几种方法,但我尝试过的方法都没有被调用。

What is the proper method to override to initialize instance variables in a UIImageView derived class?在 UIImageView 派生的 class 中,重写以初始化实例变量的正确方法是什么?

Thanks!谢谢!

There are several init functions (initWithFrame, initWithCoder, etc), and which one is called will depend on how you are loading the image view.有几个初始化函数(initWithFrame、initWithCoder 等),调用哪一个取决于您如何加载图像视图。 If you are loading it from a NIB, try initWithCoder.如果您从 NIB 加载它,请尝试使用 initWithCoder。

Alternatively, you could always use the viewDidLoad method in your controller, and have it call a custom initialization method on the view.或者,您可以始终在 controller 中使用 viewDidLoad 方法,并让它在视图上调用自定义初始化方法。 I wouldn't recommend that though in this case unless you really had to do it that way.在这种情况下,我不建议这样做,除非你真的必须这样做。

If your class is loaded as part of a nib, you can use AwakeFromNib: to initialize variables when the nib file is loaded.如果您的 class 作为 nib 的一部分加载,您可以使用 AwakeFromNib: 在加载 nib 文件时初始化变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM