0

Interface Builder を使用して静的テーブル ビューを設計しています。 シブ

オレンジ色のバーは、カスタム サブクラス化されたビューです。他の各ビューは同じように保持されますが、何らかの理由でカスタム ビューだけが消えます。後でそのプロパティにアクセスしようとすると、明示的に設定したものは 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

私のview controller.hには が@property (strong, nonatomic) IBOutlet EditableLabel *userNameEditField;あり、他のすべてのビューは正常に保持されます。なぜか自分のサブクラスだけ解放されてしまいます。

4

0 に答える 0