2013-04-23 43 views
0

我在屏幕上有一個UIView元素。它通過IBOutletradioButtonGroupView連接。我想添加一個子視圖到該元素,它不工作。子視圖永遠不會被添加。無法添加子視圖到我的UIView

這裏是我的代碼:

-(id) initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 


    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    subView.backgroundColor = [UIColor yellowColor]; 

    [self.radioButtonGroupView addSubview:subView]; 

    return self; 
} 
+0

因爲我的UITableView包含在一個nib文件中。 initWithCoder方法確實被調用,但子視圖永遠不會被添加。 – 2013-04-23 15:46:15

+6

'radioButtonGroupView'目前爲零。 – samfisher 2013-04-23 15:48:04

+0

那麼,我認爲我應該在視圖控制器中做到這一點? – 2013-04-23 15:50:38

回答

0

你可能會更好,如果你做這個元素的(而不是UIView)一UIViewController一個子類。

然後把子視圖加載代碼放在viewDidLoad方法裏面。

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; //not really needed but it doesn't hurt 

     UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
     subView.backgroundColor = [UIColor yellowColor]; 

     [self.radioButtonGroupView addSubview:subView]; 
    } 

編輯:

你說你不要在你的viewDidLoad得到self.radioButtonGroupView任何幀的信息。這幾乎是某種跡象表明您的IBOutlet未正確連接到InterfaceBuilder中的元素。你總是可以做一個簡單的測試:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; //not really needed but it doesn't hurt 

    NSLog (@"self.radioButtonGroupView: %@",self.radioButtonGroupView); 

    [self.radioButtonGroupView setHidden:YES]; 
} 

如果它仍然顯示 - 那麼它只是沒有正確連接。 IB有它的方法。簡單的 刪除IBOutlet連接並重新連接它可能會訣竅。另外:你的radioButtonGroupView不是UIView,而是UIView的一個子類,請確保它的頭文件被導入到你的MyViewController.m文件中,並且在.xib中正確定義了類。

+0

如果我在視圖控制器中使用它,那麼self.radioButtonGroupView.frame.origin.x和y以及寬度和高度一切返回0. – 2013-04-23 15:57:21

+0

最有可能的一個與IB插座的連接丟失或放錯位置。 – 2013-04-23 15:58:37

+0

它已連接!我總是有這個問題,在我看來,控制器我無法獲得IB元素的當前座標。 – 2013-04-23 15:59:46