2012-08-17 108 views
1

我有一個Storyboard視圖控制器(帶有我自己的自定義子類),其中我有一個initWithCoder自定義方法,我想用我自己的自定義對象來歡迎視圖(我想保持編程這樣的靈活性以編程方式向故事板視圖添加UIButton

代碼運行正常,直到[self.view addSubview:button];被調用,然後將其與崩潰:「在包無法加載NIB」

所有我想做的就是添加一個UIButton我的看法...

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

-(void)initLoginForm 
{ 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(100, 170, 100, 30); 
    [button setTitle:@"Login..." forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(handleLoginAttempt) forControlEvents:UIControlEventTouchUpInside]; 

    [self.view addSubview:button]; 

} 

回答

2

如何將按鈕放在viewDidLoad方法中,然後將其添加到initLoginForm中。

事情是這樣的:

-(void)viewDidLoad { 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(100, 170, 100, 30); 
    [button setTitle:@"Login..." forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(handleLoginAttempt) forControlEvents:UIControlEventTouchUpInside]; 
} 

-(void)initLoginForm { 

[self.view addSubview:button]; 

} 

或增加其在viewDidLoad然後隱藏它,然後在initLoginForm表現出來。

+0

我只是在viewDidLoad上調用initLoginForm,而在initLoginForm裏面我把它添加到視圖中,希望那可以 – Baconbeastnz 2012-08-17 01:26:06

+0

沒問題。它仍然崩潰? – Bazinga 2012-08-17 01:31:54

+0

沒錯!謝謝:) – Baconbeastnz 2012-08-17 01:41:27

相關問題