- (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,它会有什么不同。甚至不确定为什么使用自我。我解释它是因为我将当前View Controller的blueViewController属性设置为blueController实例,但为什么我会这样做。我正在阅读的这本书没有详细解释这些事情。猴子就这么做了。
答案 0 :(得分:0)
self。
答案 1 :(得分:0)
在初始化变量时,我们必须使用self。这会将blueViewController的retainCount增加到1.
self.blueViewController = blueController;
插入时也可以同时使用。结果将是相同的。
[self.view insertSubview:blueController.view atIndex:0];
[self.view insertSubview:self.blueController.view atIndex:0];
答案 2 :(得分:0)
不太了解这段代码。我怎么插入子视图blueController而不是self.blueViewController
因为你已经执行了作业:
self.blueViewController = blueController;
这两个变量是相同的,所以
[self.view insertSubview:self.blueController.view atIndex:0];
与您发布的代码相同。
如果我不使用self,它会有什么不同。甚至不确定为什么使用自我。我解释它是因为我将当前View Controller的blueViewController属性设置为blueController实例,但为什么我会这样做。我正在阅读的这本书没有详细解释这些事情。猴子就这么做了。
如果你没有分配给self.blueController
,那么你的变量只是该函数本地的一个简单变量。通过拥有属性self.blueController
并存储一个值,您可以在类的所有选择器(函数)中使用该值。
检查代码,您会看到self.blueController
也用于其他功能。例如,在某些时候,您可能会决定隐藏该子视图,或者您想要删除它等等。只有当您的类函数可以访问控制器的指针时,才能执行所有这些操作。
答案 3 :(得分:0)
blueController是一个分配和初始化的对象,而blueViewController只是一个指向BlueViewController类的指针。通过编写
self.blueViewController = blueController
你保留了blueController对象。如果你不使用self,那么你就不会在对象上进行操作,而是在行中释放它
[blueController release];
一旦再次参考,程序就会崩溃。