Objective-C:self = nil不将实例设置为null值 [英] Objective-C: self = nil doesn't set instance to null value

查看:145
本文介绍了Objective-C:self = nil不将实例设置为null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,很简单:

I've got the next code, pretty simple:

//SecondViewController.m
if(contentRvController==nil){
    contentRvController = [[ContentView alloc] 
    initWithNibName:@"ContentView" bundle:nil]; //ContentView is a custom UIViewController
    ....
    [self.view addSubview:contentRvController.view];
}
else{
    contentRvController.view.hide = YES;
    [contentRvController release];
    contentRvController = nil;
}

基本上,当从按钮启动代码时,如果UIViewController不存在,创建一个并显示它(它意味着显示在一个更大的桌面上,这是SecondViewController视图)。如果已经打开,请关闭它并删除它以释放资源。

Basically, when the code is launched from a button, if the UIViewController does not exist, create one and display it (it is meant to be displayed on a main bigger desktop view, this is the SecondViewController view). If it is already opened, close it and remove it to free resources.

现在,contentRvController是ContentView的一个实例,一个自定义的UIViewController。它有自己的关闭UIButton其IBAction是这个:

Now, contentRvController is an instance of ContentView, a custom UIViewController. It has it's own close UIButton which IBAction is this:

//ContentView.m
- (IBAction) closeView {
    self.view.hidden = YES;
    [self release];
    self = nil;
}



现在,当从SecondViewController触发时,释放contentRvController正常工作到我),视图出现和消失。但是当ContentView关闭按钮被点击时,它也关闭视图,但是当尝试再次打开它时, if(contentRvController == nil)返回 FALSE ,所以我必须点击两次按钮才能再次显示ContentView。

Now, when triggered from SecondViewController, releasing contentRvController works properly (or so it seems to me), the view appears and disappears. But when the ContentView close button is tapped it also closes the view, but when trying to open it again the if(contentRvController==nil) returns FALSE, so I have to tap twice the button to execute in order to display again the ContentView.

在我看来, self = nil; 的工作原理不同于 contentRvController = nil; ,尽管它应该都指向同一个地方,

It seems to me that self = nil; works different than contentRvController = nil; although it is supposed both points to the same place, and im lost with this.

任何想法?干杯来自México

¿Any idea? Cheers from México

推荐答案

他们的工作方式相同,但他们不会做你认为他们做的。 = 不会影响对象;它会影响指向对象的变量。将一个变量设置为nil不会更改程序中任何其他变量的值。类似地:

They work the same way, but they're not doing what you think they do. The = does not affect the object; it affects the variable pointing to the object. Setting one variable to point to nil won't change the value of any other variables in your program. Similarly:

int a = 5;
int b = a;
b = 6;
printf("A is %d and B is %d\n", a, b);

这将打印A is 5 and B is 6 - 因为将B设置为新值不改变A的值。

This will print "A is 5 and B is 6" — because setting B to a new value doesn't change the value of A.

这篇关于Objective-C:self = nil不将实例设置为null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆