如何消除这个警告?(涉及IT技术)

4

我在代码的某一行(theTextField.delegate = self;)收到了一个警告,警告信息是“将‘id’分配给不兼容的类型Alert Prompt”。

- (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个回答

8
关于文档中的此属性说明:
@property(nonatomic, assign) id<UITextFieldDelegate> delegate

这意味着你的类必须符合UITextFieldDelegate协议。
声明可能看起来像这样:
@interface MyController : NSObject <UITextFieldDelegate> {

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 协议。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接