3

カスタムUITextFieldを作成していますが、BOOLを設定していませんか??

MyCustomTextField.h

#import <UIKit/UIKit.h>

@interface OMTextField : UIControl <UITextFieldDelegate>

{
    UITextField *_theTextField;

    BOOL _hasBackGroundImage;
}

@property (nonatomic, retain)UITextField *theTextField;
@property (nonatomic) BOOL hasBackGroundImage;

@end

MyCustomTextField.m

#import "OMTextField.h"

@implementation OMTextField

@synthesize theTextField = _theTextField;
@synthesize hasBackGroundImage = _hasBackGroundImage;

- (void)dealloc 
{
    [_theTextField release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        self.theTextField = [[[UITextField alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]autorelease];
        //self.theTextField.borderStyle = UITextBorderStyleLine;
        self.theTextField.text = @"textField";
        self.theTextField.delegate = self;

        if (self.hasBackGroundImage) {
            NSLog(@"lo tienes");
        }
    if (!self.hasBackGroundImage) {
        NSLog(@"nana tienes");
    }

        [self addSubview:self.theTextField];


    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

そして私のMainVCでの呼び出し:

MyCustomTextField *textField = [[[MyCustomTextField alloc]initWithFrame:CGRectMake(400, 160, 150, 40)]autorelease];

    textField.hasBackGroundImage = YES;

    textField.backgroundColor = [UIColor grayColor];
    textField.theTextField.borderStyle = UITextBorderStyleLine;


    [self.view addSubview:textField];

うまく表示されますが、ご覧のとおり、設定は textField.hasBackGroundImage = YES;

しかし、実行すると、BOOL = NO?? のメッセージが表示されます。

nana tienes

では、何が欠けているのでしょうか?、なぜこれが YES と評価されないのですか??

ありがとう!

4

2 に答える 2

8

NSLog を initWithFrame メソッドに配置しました。これは、値がまだ NO の場合に、textField.hasBackGroundImage を設定する前の行で呼び出されます。

この値が設定されるのを確認したい場合は、次のメソッドを MyCustomTextField に追加します。

- (void)setHasBackGroundImage:(BOOL)flag
{
    _hasBackGroundImage = flag;
}

次に、このメソッドにブレークポイントを設定します。設定するとすぐにヒットするはずです。

于 2012-04-19T01:03:58.257 に答える
4

a のデフォルト値が(または)initWithFrame:であるため、「nana tienes」を出力する を呼び出しています。次に、after に設定します。BOOLFALSENOYES

に設定した後にテストを配置するとYES、「lo tienes」と出力されます。

于 2012-04-19T01:03:21.847 に答える