简体   繁体   English

iPhone-属性,ivars等

[英]iPhone - properties, ivars, etc

On this tutorial , the author has these declarations: 本教程中 ,作者具有以下声明:

on .h 在.h上

UIViewController *presentingViewController;
...

@property (retain) UIViewController *presentingViewController;

on .m 在.m上

@synthesize presentingViewController;

at some point in code, inside a block , he does: 在代码中的某个点上, 在块中 ,他执行以下操作:

self.presentingViewController = viewController;

and then 接着

[presentingViewController dismissModalViewControllerAnimated:NO];

I find this very strange. 我觉得这很奇怪。 If is is assigning the viewController to self.presentingViewController, should't it be calling 如果正在将viewController分配给self.presentingViewController,是否应该调用

    [self.presentingViewController dismissModalViewControllerAnimated:NO];

?

I have changed his code to 我已将他的代码更改为

.h 。H

@property (retain) UIViewController *presentingViewController;

.m .m

@synthesize presentingViewController = _presentingViewController;

and what I do is: 我要做的是:

self.presentingViewController = viewController;

and then 接着

    [self.presentingViewController dismissModalViewControllerAnimated:NO];

the problem is that, self.presentingViewController is nil at this line, even being declared as retain and never being released. 问题在于,self.presentingViewController在这一行上为nil,甚至被声明为keep而从未被释放。

any clues? 有什么线索吗?

thanks 谢谢

You are right that it should be: 您说的应该是对的:

[self.presentingViewController dismissModalViewControllerAnimated:NO];

However, reinstate this: 但是,请恢复该状态:

@synthesize presentingViewController;

And remove the instance decoration of a similar looking variable: 并删除外观相似的实例修饰:

UIViewController *presentingViewController;

Did you remove the instance variable UIViewController *presentingViewController when you added the instance variable UIViewController *_presentingViewController ? 添加实例变量UIViewController *_presentingViewController时是否删除了实例变量UIViewController *presentingViewController If not i would put money on you accidentally using the wrong one at some point. 如果没有,我会在某个时候不小心用错了钱。

Stepping through.. originally you had an instance variable 单步执行..最初您有一个实例变量

UIViewController *presentingViewController;

And from the property syntactic sugar you have the two accessor methods for setting and getting the variable 从属性语法糖中,您可以使用两种访问器方法来设置和获取变量

- (UIViewController *)presentingViewController;
- (void)setPresentingViewController:(UIViewController *)val;

at some point in code, inside a block, he does: 在代码的某个点上,他在一个块内执行:

[self setPresentingViewController: viewController]; // uses setter

and then 接着

[presentingViewController dismissModalViewControllerAnimated:NO]; // uses instance variable

which is fine, but you think it should be 很好,但是您认为应该

UIViewController *localPresentingViewController = [self presentingViewController]; // uses getter
[localPresentingViewController dismissModalViewControllerAnimated:NO];

which is also fine but somewhat unnecessary. 这也很好,但是有点不必要。

Then you added a new instance variable:- 然后,您添加了一个新的实例变量:

@synthesize presentingViewController = _presentingViewController;

so now you have 所以现在你有了

UIViewController *presentingViewController;
UIViewController *_presentingViewController;

// These now get / set _presentingViewController
- (UIViewController *)presentingViewController;
- (void)setPresentingViewController:(UIViewController *)val;

But almost certainly, somewhere you are confusing the two ivars (or you aren't aware you have two ivars) presentingViewController / _presentingViewController and still have a reference to the presentingViewController ivar, leading you to think your property is nil. 但是几乎可以肯定,在某个地方您混淆了两个ivars(或者您不知道自己有两个ivars)presentingViewController / _presentingViewController仍然对presentingViewController ivar进行了引用,导致您认为属性为nil。

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

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