2015-04-02 73 views
0

我在myViewController中使用自定義UIView。我有一個SampleView,這是我的自定義視圖。我有我的班級爲..在自定義UIView ios UI元素沒有響應

SampleView.h

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

,並在SampleView.m

我在ViewController中添加鹼性的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]; 
    } 
} 

現在我想改變sampleButton標題的屬性...

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

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

[self.view addSubview:sampleView]; 

SubView的按鈕文本沒有變化。請幫助我一樣。

謝謝。

回答

0

我覺得

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