UIStoryboardSegue与presentviewcontroller?

Pau*_*les 8 xcode objective-c ios

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

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

谢谢

Fog*_*ter 16

表现明智没有真正的区别.

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

使用故事板segue,对象在呈现之前从故事板中取消归档.

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

ModalViewController *modal = [[ModalViewController alloc] init];
Run Code Online (Sandbox Code Playgroud)

在你呈现它之前......

[self presentViewController:modal animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

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

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

// depends on property type etc...
modal.someProperty = @"someValue";
Run Code Online (Sandbox Code Playgroud)

使用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";
    }
}
Run Code Online (Sandbox Code Playgroud)

有区别吗?

不是真的,只是个人偏好和一些方法更容易使自己适应某些设计模式和用法.您使用的越多,您就会越了解自己喜欢哪种方法.

  • 您显示了这两种方法的属性注入,但 PresentViewController 允许进行构造函数注入。在我看来,这几乎总是更好——对于测试、代码可读性等。 (2认同)