Xcode故事板切换

3

有人能帮我理解Xcode 4.2中的新故事板吗?
我知道如何编写代码来加载另一个视图控制器,但在storyboard模式下有所不同...

我也知道有很多关于navigationcontrollers的教程,但我只想在storyboard上切换UIViewControllers。

使用普通的.xib文件,我可以使用以下代码从RootViewController切换视图..

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

但当我在storyboard模式下使用它时,它只会在SecondViewController.m中加载UIAlertView,屏幕似乎是黑色的?

任何帮助都将不胜感激,还附上了Xcode项目...

这里是zip..

-x- Jay Ruben

2个回答

13

你可以这样做:

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

别忘了给第二个视图控制器命名一个标识符,比如"second"。

否则,你可以使用segue连接两个视图控制器。按住CTRL键并从第一个视图控制器拖动到第二个视图控制器。现在你可以选择"push"并给segue一个名称来在程序中切换视图,就像这样:

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

只有设置了导航控制器,Push Segues才能正常工作。


2
您也可以这样切换方式:
// 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"];

在这里下载示例项目


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接