简体   繁体   English

分配并初始化destinationViewController

[英]Alloc and init the destinationViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@“DoSomething”]) {
        if ([segue.destinationViewController isKindOfClass:[DoSomethingVC class]]) {
            DoSomethingVC *doVC = (DoSomethingVC *)segue.destinationViewController;
doVC.neededInfo = ...; }
} 
}

In the code above, I don't understand why you need the local variable doVC. 在上面的代码中,我不明白为什么需要局部变量doVC。

If your destinationViewController already exist in the heap, why can't you pass data to it as 如果您的destinationViewController已经存在于堆中,为什么不能像这样将数据传递给它

seque.destinationViewController.neededInfo =...

If it doesn't exist in the heap, shouldn't you alloc and initialize it? 如果堆中不存在它,您是否应该分配和初始化它? Is that what this line of code is basically doing? 这行代码基本上是在做什么吗?

DoSomethingVC *doVC = (DoSomethingVC *)segue.destinationViewController;

You're allocating and instantiating an instance of DoSomethingVC class? 您正在分配和实例化DoSomethingVC类的实例吗? If so, I don't understand how this local variable is going to be kept alive since it's local. 如果是这样,我不理解该局部变量是如何保持活动的,因为它是局部的。 Also the sourceVC will also die once the seque is complete. 一旦定单完成,sourceVC也将死亡。

I just don't understand how the compiler is manipulating the memory behind this process and syncing up the data from this local variable to the destinationViewController. 我只是不明白编译器是如何处理此过程背后的内存并将数据从该局部变量同步到destinationViewController的。

segue.destinationViewController is of type UIViewController (even though it is actually a DoSomethingVC which has a base type of UIViewController ) as it stands, so it wont know about your variable neededInfo that is why you need to cast it to your DoDomethingVC . segue.destinationViewController的类型为UIViewController (即使它实际上是DoSomethingVC ,它具有UIViewController的基本类型),因此它不会知道您的变量neededInfo ,这就是为什么需要将其DoDomethingVCDoDomethingVC

It keeps segue.destinationViewController as type UIViewController to be generic, since it cant really know at compile time what this destinationViewController is going to be. 它使segue.destinationViewController作为UIViewController类型是通用的,因为它在编译时无法真正知道此destinationViewController是什么。

Going on a hunch that your grasp on inheritance is a bit shaky (not trying to be mean) but an analogy would be, if UIViewController is say type Bird and DoSomethingVC is type Duck , this prepareForSegue: method knows that a Bird will be the destinationViewController, but it doesn't know at this point whether its going to be a Duck or some other kind of bird, so you have to tell it explicitly what it is going to be (and if you get it wrong its going to do bad things, or crash) also the framework doesnt know what kinds of birds there are (since you are making them), so it cant be factored in for you 直觉你对继承的了解有点不稳定(不是要刻薄),但是可以类推,如果UIViewController的类型是BirdDoSomethingVC的类型是Duck ,则prepareForSegue:方法知道Bird将是destinationViewController。 ,但目前还不知道它是Duck还是其他鸟,因此您必须明确告诉它是什么鸟(如果弄错了,它会做坏事或崩溃),而且框架也不知道有哪种鸟(因为您是在制造它们),因此无法为您考虑它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM