简体   繁体   English

我该如何摆脱这种警告?

[英]How Can I Get Rid Of This Warning?

I'm getting a warning at line (theTextField.delegate = self;) that says "Assigning to 'id from incompatible type Alert Prompt" 我收到一行警告(theTextField.delegate = self;),上面写着“从不兼容的类型警报提示中分配'id”

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{

    if (self == [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
    {
        UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
        [theTextField setBackgroundColor:[UIColor whiteColor]]; 
        [self addSubview:theTextField];
        self.textField = theTextField;
        [theTextField release];
        CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, -25.0); 
        [self setTransform:translate];

        theTextField.backgroundColor = [UIColor clearColor];
        theTextField.borderStyle = UITextBorderStyleRoundedRect;
        theTextField.delegate = self;
    }
    return self;
}

About this property in the docs 关于docs中的这个属性

@property(nonatomic, assign) id<UITextFieldDelegate> delegate

It means, your class must conform to UITextFieldDelegate protocol. 这意味着,您的类必须符合UITextFieldDelegate协议。
Declaration could look like this 声明可能如下所示

@interface MyController : NSObject <UITextFieldDelegate> {

Try with below code and let me know if you still get the same warning. 尝试使用以下代码,如果您仍然收到相同的警告,请告诉我。

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{

    if (self == [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
    {
        UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
        [theTextField setBackgroundColor:[UIColor whiteColor]]; 

        theTextField.backgroundColor = [UIColor clearColor];
        theTextField.borderStyle = UITextBorderStyleRoundedRect;
        theTextField.delegate = self;

        self.textField = theTextField;
        [theTextField release];
        [self addSubview:textField ];
        CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, -25.0); 
        [self setTransform:translate];
    }
    return self;
}

Also check whether your class confirm with UITextFieldDelegate protocol. 还要检查您的班级是否使用UITextFieldDelegate协议进行确认。

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

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