簡體   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