【问题标题】:Why does UITextField lock up when setting itself to delegate为什么UITextField在设置自己委托时会锁定
【发布时间】:2013-03-14 11:00:52
【问题描述】:

我有一个扩展 UITextfield 的类。我也有同一类设置为自己的委托,因此选择文本字段时,我可以更改背景颜色。一旦我选择了文本字段并输入了几个字母,应用程序就会锁定并崩溃。

这是我的 .m 文件的样子

@implementation MyTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.delegate = self;

    }
    return self;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"run did begine editing");
    [self setBackgroundColor:[UIColor colorWithRed:0.204 green:0.239 blue:0.275 alpha:0.25]];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"run did end editing");
    [self setBackgroundColor:[UIColor clearColor]];
}

这是.h

@interface MyTextField : UITextField <UITextFieldDelegate>

@end

【问题讨论】:

    标签: ios objective-c uitextfield


    【解决方案1】:

    委托始终是另一个UIViewController,因为事件由定义协议的另一个类委托给它。

    当您可以访问同一个类中的所有变量和方法时,就不需要同一个类中的委托方法。

    您只需拨打[self someFunction]即可。

    当您继承 UITextField 时,您甚至不需要为 UITextField 委托定义属性。您只需将其设置为不同的 viewController。

    同样,定义协议的类只有声明,不符合协议。

    委托将是符合协议的类。

    【讨论】:

      【解决方案2】:

      订阅UITextFieldTextDidBeginEditingNotificationUITextFieldTextDidEndEditingNotification NSNotifications。在回调中检查通知对象是否为 self。然后对其执行一些操作。

      【讨论】:

        【解决方案3】:

        这个问题的讨论应该会引导你走向正确的方向self.delegate = self; what's wrong in doing that?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-29
          相关资源
          最近更新 更多