xcode Xcode故事板切换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8534731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:45:46  来源:igfitidea点击:

Xcode storyboard switch

objective-cxcodestoryboardviewcontroller

提问by Jayr

Someone help me understand the new storyboard in Xcode 4.2?
I know how to code to load another view controller but in the storyboard mode there are differences..

有人帮我理解 Xcode 4.2 中的新故事板吗?
我知道如何编码以加载另一个视图控制器,但在故事板模式中存在差异..

I also know there are a lot of tutorials about the navigationcontrollers, but I just want to switch UIViewControllers on storyboard.

我也知道有很多关于导航控制器的教程,但我只想在情节提要上切换 UIViewControllers。

With the normal .xib files I can switch views with this code from the RootViewController..

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

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

When I use it in the storyboard mode it just loads the UIAlertView on the SecondViewController.m and the screen appears to be black?

当我在故事板模式下使用它时,它只是在 SecondViewController.m 上加载 UIAlertView 并且屏幕看起来是黑色的?

Any help would be appreciated, also attached the Xcode project...

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

Here is the zip..

这是拉链..

-x- Jay Ruben

-x- 杰·鲁本

回答by Tobias Bambullis

you can do this:

你可以这样做:

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

Don't forget to give the second view controller an identifier like "second".

不要忘记给第二个视图控制器一个像“second”这样的标识符。

Otherwise you can connect both view controllers with a segue. Hold CTRL an drag from the first to the second view Controller. Now you can choose "push" and give the segue a name to switch the View programmatically like this:

否则,您可以使用 segue 连接两个视图控制器。按住 CTRL 从第一个视图控制器拖动到第二个视图控制器。现在您可以选择“push”并为 segue 命名以编程方式切换视图,如下所示:

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

Push Segues will only work if a navigation controller is set.

Push Segues 只有在设置了导航控制器时才有效。

回答by justinkoh

You can also switch this way:

你也可以这样切换:

// 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"];

Download the sample project here.

此处下载示例项目