早期发布的静态表视图中的自定义视图

时间:2013-11-07 20:27:22

标签: ios objective-c automatic-ref-counting

我正在使用Interface Builder来设计静态表视图: xib http://i43.tinypic.com/296hptt.png

橙色条是自定义子视图。每个其他视图以相同的方式保留,但由于某种原因,只有我的自定义视图消失。尝试访问它的属性后来导致我明确设置的东西为null,但没有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;,其他每个视图都保留不变。出于某种原因,只发布了我的子类。

0 个答案:

没有答案