2013-02-10 81 views
-1

應用程序應根據數據庫中的某些數據轉發到另一個視圖控制器。數據庫工作正常,但我無法讓應用轉發到第二個視圖控制器。 下面是我的代碼。如何使用代碼推送到新的視圖控制器?

ssSecondViewController *tempView =[[ssSecondViewController alloc] init]; 
[self.navigationController pushViewController:tempView animated:YES]; 

我沒有收到任何錯誤,但是當我運行代碼屏幕轉發到一個空白的黑色視圖?

+1

我應該補充說我正在使用故事板和xcode 4.5 – user1362262 2013-02-10 01:11:11

回答

3

如果您已經在對故事板的viewController,那麼你可以像下面的代碼&您必須設置標識符viewControler在故事板

ssSecondViewController *tempView =[self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"]; 

設置你的viewController ID在這裏被分配給它,在下面的圖像標識爲「MenuViewController」

enter image description here

,如果它是的.xib文件,然後

ssSecondViewController *tempView =[[ssSecondViewController alloc] initWithNibName:@"yourIdentifier" bundle:nil]; 

,如果你只是想和programmetically添加新的viewController不使用已有的viewController,那麼你有initWithFrame來初始化它,然後它顯示的viewController

ssSecondViewController *tempView =[[ssSecondViewController alloc] initWithFrame:CGRectMake(0,0,320,480)]; 

ssSecondViewController *tempView =[[ssSecondViewController alloc] init];此行只是分配的viewController,它不有任何地點來繪製它。

+0

非常感謝。你的第一個答案(storyboard)就是我所需要的。像魅力一樣工作。真棒! – user1362262 2013-02-10 12:36:12

0

您需要使用

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 

因此,對於你的例子那就是:

ssSecondViewController *tempView =[[ssSecondViewController alloc] initWithNibName:@"ssSecondViewController" bundle:nil]; 
0

故事板你通常實際上並沒有推壓,你Segue公司在故事板使用segueIdentifiers設置:

[self performSegueWithIdentifier:@"ssSecondSegue" sender:self]; 

然後在你的中處理數據傳遞和其他setter

相關問題