2013-10-31 139 views
3

我是iOS開發新手。我正嘗試從xib文件連接到故事板。我的xib視圖控制器有一個「登錄」,如果成功應該連接到故事板。我用Google搜索和搜索計算器上的解決方案,我用這個代碼是無處不在給出:無法從xib調用故事板

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    YourViewController * yourView = (YourViewController *)[storyboard instantiateViewControllerWithIdentifier:@"identifier ID"]; 

我創造了我的故事板的標識符爲好。但是,無論我嘗試什麼,我都不會被重定向到故事板。當登錄完成後,我回到我的主視圖控制器(xib)。我應該重定向到故事板。這裏是我的代碼看起來像在一個名爲ProfileTabView.m文件:

-(void) loginViewDidFinish:(NSNotification*)notification 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil]; 
    ProfileTabView * yourView = (ProfileTabView *)[storyboard instantiateViewControllerWithIdentifier:@"myID"]; 

} 

我已經Implemeted一個在功能這段代碼中被調用一次登錄成功。然而,故事板「故事板」永遠不會被調用。我做對了嗎?我應該在其他地方寫這個代碼嗎?

非常感謝任何幫助:)

+1

您需要通過任一推 「yourView」 .view添加到視圖層次/本/添加子視圖。 loginViewDidFinish中的那部分代碼在哪裏? – San

回答

3

一個步驟:提出新的視圖控制器...

-(void) loginViewDidFinish:(NSNotification*)notification 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil]; 
    ProfileTabView * yourView = (ProfileTabView *)[storyboard instantiateViewControllerWithIdentifier:@"myID"]; 

    // it might be better to rename 'ProfileTabView' as 'ProfileTabViewController' 
    // and 'yourView' as 'profileTabVC', by convention. Anyway ... 

    [self.presentViewController:yourView animated:YES completion:^{}]; 
} 

如果登錄VC是一個導航控制器,那麼你會推新的VC:

[self.navigationController pushViewController:yourView animated:YES]; 
0
-(void) loginViewDidFinish:(NSNotification*)notification 
{ 
NSNotificationCenter.defaultCenter().removeObserver(self) 
let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc = storyboard.instantiateViewControllerWithIdentifier("ViewControllerID") as UIViewController 
    self.presentViewController(vc, animated: true, completion: nil) 
}