iPhone-属性、IVAR等

iPhone-属性、IVAR等,iphone,ios,objective-c-blocks,Iphone,Ios,Objective C Blocks,关于,作者有以下声明: on.h on.m 在代码中的某个点,在一个块中,他会: self.presentingViewController = viewController; self.presentingViewController = viewController; [self setPresentingViewController: viewController]; // uses setter 然后 [presentingViewController dismissModalVi

关于,作者有以下声明:

on.h

on.m

在代码中的某个点,在一个块中,他会:

self.presentingViewController = viewController;
self.presentingViewController = viewController;
[self setPresentingViewController: viewController]; // uses setter
然后

[presentingViewController dismissModalViewControllerAnimated:NO];
    [self.presentingViewController dismissModalViewControllerAnimated:NO];
[presentingViewController dismissModalViewControllerAnimated:NO]; // uses instance variable
我觉得这很奇怪。如果is正在将viewController分配给self.presentingViewController,它是否应该调用

    [self.presentingViewController dismissModalViewControllerAnimated:NO];
?

我把他的密码改成了

h

m

我所做的是:

然后

[presentingViewController dismissModalViewControllerAnimated:NO];
    [self.presentingViewController dismissModalViewControllerAnimated:NO];
[presentingViewController dismissModalViewControllerAnimated:NO]; // uses instance variable
问题是,self.presentingViewController在这一行是nil,甚至被声明为retain并且从未被释放

有什么线索吗


谢谢

你说得对,应该是:

[self.presentingViewController dismissModalViewControllerAnimated:NO];
但是,恢复这一点:

@synthesize presentingViewController;
并删除外观类似的变量的实例装饰:

UIViewController *presentingViewController;

添加实例变量UIViewController*\u presentingViewController时,是否删除了实例变量UIViewController*presentingViewController?如果不是的话,我会把钱放在你身上,在某个时候不小心用错了

穿过。。最初您有一个实例变量

UIViewController *presentingViewController;
- (UIViewController *)presentingViewController;
- (void)setPresentingViewController:(UIViewController *)val;
从属性语法中,您有两种用于设置和获取变量的访问器方法

UIViewController *presentingViewController;
- (UIViewController *)presentingViewController;
- (void)setPresentingViewController:(UIViewController *)val;
在代码中的某个点,在一个块中,他会:

self.presentingViewController = viewController;
self.presentingViewController = viewController;
[self setPresentingViewController: viewController]; // uses setter
然后

[presentingViewController dismissModalViewControllerAnimated:NO];
    [self.presentingViewController dismissModalViewControllerAnimated:NO];
[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;

但几乎可以肯定的是,在某个地方你混淆了这两个ivar,或者你不知道你有两个ivar存在于IEWController/\u存在于IEWController,并且仍然有一个对存在于IEWController ivar的引用,这让你认为你的财产是零。

@hooleyhoop-是的,它们创建实例变量presentingViewController和属性presentingViewController。它们将创建的视图控制器指定给属性版本,然后使用实例版本显示/取消模型视图控制器。由于实例变量从未链接到属性,因此这将不起作用。此外,实例变量和属性的名称完全相同,这是非常糟糕的风格。不需要实例变量,因此我建议OP删除它,只使用变量的属性版本。嗨,我根本没有声明ivar,我只是使用@synthesis presentingViewController=\u presentingViewController;我想这将创建下划线ivar。现在它运行良好。该教程中的代码有很多类似的小问题,我花了大量时间来解决这些问题。不管怎样,你的答案是+1。当你说“我想无论如何都会创建下划线ivar”时,听起来你好像没有意识到这就是合成所做的一切。它在编译时创建var、getter方法和setter方法,只是为了节省键入的时间。如果var、getter或setter已经存在,因为您键入了它,那么它不会再添加它。合成foo;创建一个名为foo的ivar,即-idfoo;方法和方法-voidsetFoo:idarg;方法使用@synthesis上的选项,您可以为其中任何一个使用自定义名称,但除非您有充分的理由,否则您应该使用默认名称。属性只是遗留objective-c的语法糖。在现代objective-c中,我们构建今天的iOS应用程序时使用的那种类型,用属性声明一个同名的ivar是不必要的,正如OP所展示的那样,它令人困惑。查看@claireware中的运行时差异-我故意避免说任何与此相矛盾的话。如果我有我的道歉,请随意编辑我的答案。我不同意您的观点,即OP的混淆表明声明同名ivar和属性是混淆的。我读到这个问题是因为OP想要一个由不同名字的iVar支持的房产。在我看来,没有明确宣布支持该地产的iVar只会增加混乱,但我谨慎地没有这样说。在方法声明'-intfoo'中,返回类型是不必要的,但这并不意味着它是坏的、坏的样式。