繁体   English   中英

iPhone-属性,ivars等

[英]iPhone - properties, ivars, etc

本教程中 ,作者具有以下声明:

在.h上

UIViewController *presentingViewController;
...

@property (retain) UIViewController *presentingViewController;

在.m上

@synthesize presentingViewController;

在代码中的某个点上, 在块中 ,他执行以下操作:

self.presentingViewController = viewController;

接着

[presentingViewController dismissModalViewControllerAnimated:NO];

我觉得这很奇怪。 如果正在将viewController分配给self.presentingViewController,是否应该调用

    [self.presentingViewController dismissModalViewControllerAnimated:NO];

我已将他的代码更改为

。H

@property (retain) UIViewController *presentingViewController;

.m

@synthesize presentingViewController = _presentingViewController;

我要做的是:

self.presentingViewController = viewController;

接着

    [self.presentingViewController dismissModalViewControllerAnimated:NO];

问题在于,self.presentingViewController在这一行上为nil,甚至被声明为keep而从未被释放。

有什么线索吗?

谢谢

您说的应该是对的:

[self.presentingViewController dismissModalViewControllerAnimated:NO];

但是,请恢复该状态:

@synthesize presentingViewController;

并删除外观相似的实例修饰:

UIViewController *presentingViewController;

添加实例变量UIViewController *_presentingViewController时是否删除了实例变量UIViewController *presentingViewController 如果没有,我会在某个时候不小心用错了钱。

单步执行..最初您有一个实例变量

UIViewController *presentingViewController;

从属性语法糖中,您可以使用两种访问器方法来设置和获取变量

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

在代码的某个点上,他在一个块内执行:

[self setPresentingViewController: viewController]; // uses setter

接着

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

很好,但是您认为应该

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

这也很好,但是有点不必要。

然后,您添加了一个新的实例变量:

@synthesize presentingViewController = _presentingViewController;

所以现在你有了

UIViewController *presentingViewController;
UIViewController *_presentingViewController;

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

但是几乎可以肯定,在某个地方您混淆了两个ivars(或者您不知道自己有两个ivars)presentingViewController / _presentingViewController仍然对presentingViewController ivar进行了引用,导致您认为属性为nil。

暂无
暂无

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

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