自定义UIView中的ios UI元素无响应

时间:2015-04-02 06:43:54

标签: uiview uiviewcontroller ios8

我在myViewController中使用自定义UIView。我有一个SampleView,这是我的自定义视图。我的班级是......

SampleView.h

@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (strong, nonatomic) IBOutlet UIButton *sampleButton;

并在SampleView.m

我添加了一个基本的init

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    UIView *view = nil;
    NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"SampleView"
                                                     owner:self
                                                   options:nil];
    for (id object in objects) {
        if ([object isKindOfClass:[UIView class]]) {
            view = object;
            break;
        }
    }

    if (view != nil) {
        _mainView = view;
        view.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:view];
        [self setNeedsUpdateConstraints];
    }
}

现在在我的ViewController中,我想更改sampleButton的标题属性...

SampleView *sampleView = [[SampleView alloc]init];

sampleView.sampleButton.titleLabel.text = @"Hello";

[self.view addSubview:sampleView];

SubView的按钮文字没有变化。请帮我一下。

感谢。

1 个答案:

答案 0 :(得分:0)

我认为

- (void)commonInit
{
   //UIView *view = nil;
   UIView *view=[[UIView all]init];
   ........
}