0

我正在使用 Interface Builder 设计一个静态表格视图: 西布

橙色条是自定义子类视图。每个其他视图都以相同的方式保留,但由于某种原因,只有我的自定义视图消失了。稍后尝试访问它的属性会导致我明确设置的内容为空,但没有 EXEC_BAD_ACCESS。

自定义视图代码:

#import "EditableLabel.h"

@implementation EditableLabel
@synthesize font;
@synthesize maxSize;
@synthesize textColor;

- (void)awakeFromNib{
    [self innerInit];
}
- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"got init");
    self = [super initWithFrame:frame];
    if (self) {
        [self innerInit];
    }
    return self;
}

-(void)innerInit{
    self.font = [UIFont systemFontOfSize:[UIFont systemFontSize]]; //default sys font, set this for pretty
    self.maxSize = self.frame.size; //set this for pretty too
    self.textColor = [UIColor blackColor];
    self.isEditting = NO;

    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.button.userInteractionEnabled=YES;
    self.button.hidden=NO;
    self.button.titleLabel.font=font;
    [self.button setTitleColor:textColor forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];
    [self.button setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [self.button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self addSubview:self.button];

    self.editField = [[UITextField alloc] initWithFrame:self.button.frame];
    self.editField.font=font;
    self.editField.textColor = textColor;
    self.editField.userInteractionEnabled=YES;
    self.editField.hidden=YES;
    self.editField.borderStyle = UITextBorderStyleRoundedRect;
    [self.editField addTarget:self action:@selector(finishEditting) forControlEvents:UIControlEventEditingDidEnd];
    [self.editField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [self.editField setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [self addSubview:self.editField];

    [self bringSubviewToFront:self.button];
}

-(void)setTextFieldDelegate:(id)viewcontroller{
    self.editField.delegate = viewcontroller;
}

-(void)edit{
    [self flipWithCompletionBlock:^(BOOL done){
        if (done){
            self.isEditting=YES;
            self.button.enabled=NO;
        }
    }];
}

-(void)finishEditting{
    [self flipWithCompletionBlock:^(BOOL done){
        if (done){
            self.isEditting=NO;
            self.button.enabled=YES;
            [self.button setTitle:self.editField.text forState:UIControlStateNormal];
        }
    }];
}

-(NSString*)text{
    return self.button.titleLabel.text;
}
-(void)setText:(NSString*)text{
    self.editField.text=text;
    [self.button setTitle:text forState:UIControlStateNormal];
}

-(UIColor*)backgroundColor{
    return self.button.backgroundColor;
}

-(void)setBackgroundColor:(UIColor *)backgroundColor{
    [self.button setBackgroundColor:backgroundColor];
    [self.editField setBackgroundColor:backgroundColor];
}

-(void)setAlignmentVertical:(UIControlContentVerticalAlignment)vert horizontalAlignment:(UIControlContentHorizontalAlignment)horiz{
    [self.button setContentHorizontalAlignment:horiz];
    [self.button setContentVerticalAlignment:vert];
    [self.editField setContentHorizontalAlignment:horiz];
    [self.editField setContentVerticalAlignment:vert];
}

- (void)flipWithCompletionBlock:(void (^)(BOOL))block{
    [UIView transitionWithView:self
                      duration:.3
                       options:UIViewAnimationOptionTransitionFlipFromTop
                    animations:^{
                        if (self.isEditting){
                            self.editField.hidden=YES;
                            self.button.hidden=NO;
                            [self bringSubviewToFront:self.button];
                        }
                        else {
                            self.button.hidden=YES;
                            self.editField.hidden=NO;
                            [self bringSubviewToFront:self.editField];
                        }
                    } completion:block];

}

@end

在我的视图 controller.h 中,我有@property (strong, nonatomic) IBOutlet EditableLabel *userNameEditField;,并且所有其他视图都保留得很好。出于某种原因,只发布了我的子类。

4

0 回答 0