本文介绍了带有插座初始化的iOS自定义UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 UITextField ,几乎没有自定义。
我创建了新的自定义类CustomTextField。在IB中,我为每个自定义 UITextField 创建了 outlet 。我运行代码并且自定义无法正常工作。

I need UITextField with little customization.I've created new custom class CustomTextField. In IB I created outlet for each of my custom UITextField. I run the code and customization was not working.

要进行检查我在初始化过程中放置​​了断点 initWithFrame 自定义 UITextField 。断点没有被击中。我想必须在每个View Controller中添加额外的 init ,但我不确定究竟是什么。

To make check I put breakpoint on initialization procedure initWithFrame in the custom UITextField. The breakpoint was not hit. I suppose that additional init must be made in each View Controller, but I am not sure what exactly.

As我创建了插座我假设 init 将自动生成,但因为这是一个自定义类,所以必须手动 init 有这样的结构:

As I created outlet I suppose that init will be made automatically, but because this is a custom class it must be init manually with construction like that:

CustomTexTField *textField = [[CustomTextField alloc] initWithFrame:customFrame];
[self.view addSubView:textField];

所以每个我都需要额外的 init 查看控制器以及如何操作?

So do I need additional init in each View controller and how to do that?

推荐答案

从IB或故事板初始化视图时, initWithCoder 是使用的初始化程序,而不是 initWithFrame ,这就是断点未被命中的原因。将自定义代码放在 initWithCoder 方法中。例如:

When initializing a view from IB or a storyboard, initWithCoder is the initializer used, not initWithFrame, and is why the breakpoint is not being hit. Put the customization code in the initWithCoder method. E.g.:

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {

        // Put customization code here...

    }
    return self;
}

这篇关于带有插座初始化的iOS自定义UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:35