xcode UIStoryboardSegue 与presentviewcontroller?

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

UIStoryboardSegue versus presentviewcontroller?

iosobjective-cxcode

提问by Paulo Sigales

Someone can explain the difference between use of UIStoryboardSegue modalversus programmatically presentViewController?

有人可以解释使用UIStoryboardSegue modal与编程之间的区别presentViewController吗?

Use UIStoryboardSegueis only to facilitate? Or have some performance benefit?

使用UIStoryboardSegue只是为了方便?或者有一些性能优势?

Thank you

谢谢

回答by Fogmeister

Performance wise there is no real difference.

性能方面没有真正的区别。

The main difference is where the new view controller is created.

主要区别在于新视图控制器的创建位置。

With the storyboard segue the object is unarchived from the storyboard before it is presented.

使用情节提要转场,对象在呈现之前从情节提要中取消归档。

In code you would have to create the new view controller like...

在代码中,您必须创建新的视图控制器,例如...

ModalViewController *modal = [[ModalViewController alloc] init];

before you present it...

在你介绍它之前......

[self presentViewController:modal animated:YES completion:nil];

Both of them allow you to inject properties in different ways too.

它们都允许您以不同的方式注入属性。

Using code you would add the following above...

使用代码,您将在上面添加以下内容...

// depends on property type etc...
modal.someProperty = @"someValue";

When using a segue you would do this...

使用 segue 时,你会这样做......

- (void)prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"modalSegue"]) {
        // the view controller is already created by the segue.
        // just grab it here first
        ModalViewController *controller = segue.destinationViewController;

        controller.someProperty = @"someValue";
    }
}

Is there a difference?

有区别吗?

Not really, just personal preference and some approaches lend themselves more easily to certain design patterns and usages. The more you use both the more you'll learn which approach you prefer.

并非如此,只是个人喜好和某些方法更容易适用于某些设计模式和用法。您使用的越多,您就越会了解您更喜欢哪种方法。