我该如何摆脱这种警告?

时间:2011-04-24 08:12:10

标签: iphone objective-c

我收到了一行警告(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;
}

2 个答案:

答案 0 :(得分:8)

关于the docs

中的此属性
@property(nonatomic, assign) id<UITextFieldDelegate> delegate

这意味着,您的班级必须符合UITextFieldDelegate协议 宣言可能看起来像这样

@interface MyController : NSObject <UITextFieldDelegate> {

答案 1 :(得分:0)

尝试使用以下代码,如果您仍然收到同样的警告,请告诉我。

- (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;
}

同时检查您的班级是否使用UITextFieldDelegate协议确认。