为什么UITextField子类背景图像没有显示?

时间:2013-03-05 23:29:24

标签: objective-c xcode oop interface-builder

我在Interface Builder中设置了一个带有背景图像的UITextField。背景显示正常,但是当我将类名称切换(在IB中)到我的UITextField子类(ValidatedTextField)时,背景图像不显示。任何人都可以发现为什么我的UITextView子类不应该有图像的原因吗?

其他信息
不知道这是否有帮助,但IB也给了我一些麻烦 - 有时不允许我更改这些文本字段的类名。

//ValidatedTextField.h
#import <UIKit/UIKit.h>
#import "MBValidated.h"

@interface ValidatedTextField : UITextView <MBValidated>

// the maximum characters allowed
@property (assign, nonatomic) int mbMaxLength;

// an visual indicator of the validation state (checkmark, etc)
@property (strong, nonatomic) UIImageView *mbStatusImageView;

// whether the field can be empty
@property (assign, nonatomic) BOOL mbIsRequired;

// whether we have succesfully validated
@property (assign, nonatomic) BOOL mbIsValid;

// validate and update stored validated state
-(BOOL)mbValidate;

@end




// ValidatedTextField.m
#import "ValidatedTextField.h"

@implementation ValidatedTextField

@synthesize mbMaxLength, mbStatusImageView;

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

// set any default values here
-(void)mbSetDefaults
{
    self.mbIsRequired = YES;
    self.mbIsValid = YES;
}

-(BOOL)mbValidate
{    
    // validate length
    if(self.text.length > self.mbMaxLength) self.mbIsValid = NO;

    // validate empty or filled
    if(self.text.length == 0 && self.mbIsRequired == YES) self.mbIsValid = NO;

    return self.mbIsValid;
}

- (void)awakeFromNib
{
    // set defaults
    [self mbSetDefaults];
}

@end


// MBValidated Protocol
#import <Foundation/Foundation.h>

@protocol MBValidated <NSObject>

// whether the field can be empty
@property (assign, nonatomic) BOOL mbIsRequired;

// whether we have succesfully validated
@property (assign, nonatomic) BOOL mbIsValid;

// validate the item
-(BOOL)mbValidate;

@end

1 个答案:

答案 0 :(得分:1)

你应该是UITextField的子类,而不是UITextView。