xcode 故事板和切换视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8319002/
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
Storyboard and Switch Views?
提问by user964627
I have an iPad app that uses the storyboard board feature and then I have a separate .xib file for another view. I can switch to the separate view and its fine:
我有一个使用故事板功能的 iPad 应用程序,然后我有一个单独的 .xib 文件用于另一个视图。我可以切换到单独的视图,它很好:
-(IBAction)SecondView:(id)sender{
SecondView *Second = [[SecondView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Second animated:NO];
}
But when I am in the Second View and try going back I do everything the same just with the first view controller, but It just goes to a black screen:
但是当我在第二个视图中并尝试返回时,我对第一个视图控制器执行相同的操作,但它只是进入黑屏:
-(IBAction)FirstView:(id)sender{
FirstView *First = [[FirstView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:First animated:NO];
}
What do you guys think? Am I doing something wrong? What is the best way to switch views?
你们有什么感想?难道我做错了什么?切换视图的最佳方法是什么?
回答by amattn
initWithNibName:bundle
is for loading nib or xib files.
initWithNibName:bundle
用于加载 nib 或 xib 文件。
If you are loading from a storyboard, you need to use
如果您从故事板加载,则需要使用
FirstView *First= [self.storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER"];
IDENTIFIER
is defined in your storyboard, it's in the Utilities, Attributes Inspector on the right side.
IDENTIFIER
在您的故事板中定义,它位于右侧的实用程序、属性检查器中。
HOWEVERyour real problem is that you shouldn't be loading from the storyboard at all. you should just be calling
但是,您真正的问题是根本不应该从故事板加载。你应该只是打电话
[self dismissModalViewControllerAnimated:YES];
That call will clean up the presentModalViewController:animated:
that you used to put the modal view controller up in the first place.
该调用将首先清理presentModalViewController:animated:
您用于放置模态视图控制器的那些。
回答by rob mayoff
You presented SecondView
using presentModalViewController:animated:
, so you need to dismiss it using dismissModalViewControllerAnimated:
.
您展示了SecondView
using presentModalViewController:animated:
,因此您需要使用 关闭它dismissModalViewControllerAnimated:
。
- (IBAction)FirstView:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}