2011-12-16 197 views
3

有人幫我理解Xcode 4.2中的新故事板嗎?
我知道如何代碼加載另一個視圖控制器,但在故事板模式也有差別..Xcode故事板開關

我也知道有很多關於navigationcontrollers教程,但我只是想打開故事板UIViewControllers。

隨着正常的.xib文件,我可以切換視圖從RootViewController的這個代碼..

 
SecondViewController *Second = [[SecondViewController alloc] initWithNibName:nil bundle:nil]; 
[self presentModalViewController:Second animated:YES]; 

當我使用它在故事板模式,它​​只是加載的SecondViewController.m和屏幕的UIAlertView中似乎是黑色的?

任何幫助,將不勝感激,還附上了Xcode項目...

Here is the zip ..

-X-周杰倫魯

回答

13

,你可以這樣做:

SecondViewController *second= [self.storyboard instantiateViewControllerWithIdentifier:@"second"]; 
[self presentModalViewController:second animated:YES]; 

不要忘記給第二個視圖控制器一個標識符,如「第二個」。

否則,您可以連接兩個視圖控制器與segue。按住CTRL拖動第一個視圖到第二個視圖Controller。現在,您可以選擇「推」並給予SEGUE的名稱在程序中切換視圖是這樣的:如果一個導航控制器設置

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

推塞格斯纔會工作。

+0

這幫了我很多,非常感謝! – 2011-12-16 14:12:29

3

您還可以切換這樣:

// get the view that's currently showing 
UIView *currentView = self.view; 
// get the the underlying UIWindow, or the view containing the current view 
UIView *theWindow = [currentView superview]; 

UIView *newView = aTwoViewController.view; 

// remove the current view and replace with myView1 
[currentView removeFromSuperview]; 
[theWindow addSubview:newView]; 

// set up an animation for the transition between the views 
CATransition *animation = [CATransition animation]; 
[animation setDuration:0.5]; 
[animation setType:kCATransitionPush]; 
[animation setSubtype:kCATransitionFromRight]; 
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 

[[theWindow layer] addAnimation:animation forKey:@"SwitchToView2"]; 

下載sample project here