如何在 xcode 4.2 故事板中正确使用模态视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8261360/
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:32:48  来源:igfitidea点击:

How to properly use modal view controller with the xcode 4.2 storyboard

iosxcodemodal-dialogstoryboardsegue

提问by Warkst

I was wondering how to properly use the storyboard to put up a view controller modally. Personally I prefer working with xibs, but it seems that the storyboard is gaining popularity and will be the way to go in the future.

我想知道如何正确使用情节提要以模态方式放置视图控制器。我个人更喜欢与xibs合作,但似乎故事板越来越受欢迎,并将成为未来的发展方向。

The way I would normally put up a view controller modally would be like this: let's say we have ViewControllerA (A for short) and ViewControllerB (B for short). I would then normally put a protocol in B.h specifying the delegate method when B wants to be dismissed and add the id<theProtocol> delegatefield as an assignproperty. Assuming i'm busy in A and I want to present B modally, I would write:

我通常以模态方式设置视图控制器的方式是这样的:假设我们有 ViewControllerA(简称 A)和 ViewControllerB(简称 B)。然后我通常会在 Bh 中放置一个协议,当 B 想要被解除时指定委托方法并将该id<theProtocol> delegate字段添加为assign属性。假设我在 A 很忙,我想以模态呈现 B,我会写:

B* b = [[B alloc] initWithNibName:@"B" bundle:nil];
b.delegate = self;
[self presentModalViewController:B animated:YES];

Using the storyboard, I know it's possible to put up a different view controller in a modal way by ctrl-dragging from a button to a viewcontroller and selecting modal as transition type. I'm just wondering though; where do I set the delegate of the new view controller? What's the correct practice of passing things to your modal view controller? I don't really know what the whole deal with Segues is...

使用故事板,我知道可以通过按住 ctrl 从按钮拖动到视图控制器并选择模态作为转换类型以模态方式放置不同的视图控制器。我只是想知道;我在哪里设置新视图控制器的委托?将事物传递给模态视图控制器的正确做法是什么?我真的不知道与 Segues 的整个交易是什么......

采纳答案by d.lebedev

Take a look at thistutorial

看看这个教程

According to it, you should set the delegate as follows:

根据它,您应该按如下方式设置委托:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddPlayer"])
    {
        UINavigationController *navigationController = 
          segue.destinationViewController;
        PlayerDetailsViewController 
          *playerDetailsViewController = 
            [[navigationController viewControllers] 
              objectAtIndex:0];
        playerDetailsViewController.delegate = self;
    }
}

Where @"AddPlayer" is the name of your 'modal' segue

其中@"AddPlayer" 是您的“模态”segue 的名称

回答by Jai Srivastav

Instead of using the navigation controller you could directly use the UIStoryboardSegueobject passed in prepareForSegue. It has a property called destinationViewControllerwhich is the view controller that is being instantiated. I find that a lot cleaner. This is an example.

您可以直接使用UIStoryboardSegue传入的对象,而不是使用导航控制器prepareForSegue。它有一个称为destinationViewController正在实例化的视图控制器的属性。我觉得这样干净多了。这是一个例子。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddPlayer"])
    {
        PlayerDetailsViewController 
          *playerDetailsViewController = 
            (PlayerDetailsViewController *) segue.destinationViewController;

        playerDetailsViewController.delegate = self;
    }
}

IMO I think that storyboards are great because they function like a blueprint of your application. Also I've never liked nibs. =D

IMO 我认为故事板很棒,因为它们的功能就像您的应用程序的蓝图。另外我从来不喜欢笔尖。=D