繁体   English   中英

插入子视图 - iPhone

[英]Inserting subView - iPhone

- (void)viewDidLoad {

    BlueViewController *blueController = [[BlueViewController alloc] initWithNibName@"BlueView" bundle:nil];
    self.blueViewController = blueController; //blueViewController set to var above

    [self.view insertSubview:blueController.view atIndex:0];
    [blueController release];
    [super viewDidLoad];
}

不太了解这段代码。 我为什么要插入子视图 blueController 而不是 self.blueViewController

如果我不使用 self. 甚至不确定为什么要使用 self 。 我将其解释为将当前视图 Controller 的 blueViewController 属性设置为 blueController 实例,但我为什么要这样做。 我正在阅读的那本书没有详细解释这些事情。 这几乎是猴子做的。

如果您指的是 class 的 object,则使用 self。

在初始化变量时,我们必须使用 self. 这会将 blueViewController retainCount 增加到 1。

self.blueViewController = blueController;

插入时也可以同时使用。 结果将是相同的。

[self.view insertSubview:blueController.view atIndex:0];
[self.view insertSubview:self.blueController.view atIndex:0];

不太了解这段代码。 我为什么要插入子视图 blueController 而不是 self.blueViewController

因为你已经执行了任务:

 self.blueViewController = blueController;

这两个变量是相同的,所以

 [self.view insertSubview:self.blueController.view atIndex:0];

将与您发布的代码相同。

如果我不使用 self. 甚至不确定为什么要使用 self 。 我将其解释为将当前视图 Controller 的 blueViewController 属性设置为 blueController 实例,但我为什么要这样做。 我正在阅读的那本书没有详细解释这些事情。 这几乎是猴子做的。

如果您不分配给self.blueController ,那么您的变量只是该 function 的一个简单变量。 通过拥有一个self.blueController属性并在其中存储一个值,您可以在 class 的所有选择器(函数)中使用该值。

检查代码,您会看到self.blueController也用于其他功能。 例如,在某些时候,您可能决定要隐藏该子视图,或者您想将其删除等。所有这些只有在您的 class 函数可以访问 controller 的指针时才能执行。

blueController 是分配和初始化的 object 而 blueViewController 只是指向 BlueViewController class 的指针。通过编写

self.blueViewController = blueController

您保留 blueController object。如果您不使用 self 您将不会对 object 进行评级,并且在您在线发布后

[blueController release];

一旦你再次引用它,你的程序就会崩溃。

暂无
暂无

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

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