2011-05-20 65 views
0

好吧我正在尋找這個錯誤,因爲2小時,我只是不能弄明白,請幫助我。 我有以下情況我有2個viewcontroller。 一個呈現另一個像這樣的modalview。消息發送到解除分配的實例!不能找到錯誤

SearchViewController *searchViewController = [[SearchViewController alloc]init]; 
[searchViewController setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; 
searchViewController.delegate = self; 
searchViewController.senderTag = [sender tag]; 
[self presentModalViewController:searchViewController animated:YES]; 
[searchViewController release]; 
在我searchviewcontroller

我這樣做在.m文件我合成它.h文件

BSKmlResult *selectedAirport; 
@property (nonatomic, retain) BSKmlResult *selectedAirport; 

然後將其設置這樣

selectedAirport = [self.airportList objectAtIndex:indexPath.row]; 

,然後再釋放這裏

- (void)dealloc { 
    [selectedAirport release]; 
    [super dealloc]; 
} 

在我SearchViewController的代表梅索德這是在第一 視圖 - 控制實現,其中我也介紹了SearchViewController 我有以下

if (controller.selectedAirport) { 
      if (departureAirport) { 
       [departureAirport release]; 
      } 

     departureAirport = [controller.selectedAirport copy]; 
} 

[self dismissModalViewControllerAnimated:YES]; 

我縮小在錯誤發生它在我SearchViewController 的的dealloc [selectedAirport release];

,但我無法揣摩出我的錯誤是 請幫

回答

9
selectedAirport = [self.airportList objectAtIndex:indexPath.row]; 

您的arent這裏保留selectedAirport。

將其更改爲

self.selectedAirport = [self.airportList objectAtIndex:indexPath.row]; 

既然你不可能發現它,也許你不知道這...

如果你不通過self.memberVariable訪問成員變量,你不是在訪問其屬性。因此,它沒有得到保留。

Ofcourse,你也可以說

selectedAirport = [[self.airportList objectAtIndex:indexPath.row] retain]; 

但保留它有什麼用你的財產則...

+0

你是完全正確的!將其更改爲self.selectedAirport,它的功能就像魅力一樣!我確實不知道這一點,我對這一切都很陌生。還有一個問題我什麼時候使用自我。什麼時候不?有沒有規則?礦石我是否總是使用自我。 ? – bllubbor 2011-05-20 14:48:03

+0

@bllubbor - 我總是使用自我,從來沒有任何問題。 – Rayfleck 2011-05-20 14:50:51

+0

它很簡單,只要你想訪問屬性就使用自己。但我個人使用自己來「設置」,而不是「得到」。 – 2011-05-20 14:56:28

3

您需要使用的自我。通過合成的方法運行它,以獲得保留。

self.selectedAirport = [self.airportList objectAtIndex:indexPath.row]; 
+0

!非常感謝 – bllubbor 2011-05-20 14:49:16

2

我知道這篇文章是相當古老,但只是想添加一些有用的東西。在上面的例子中,成員變量名稱和屬性名稱是相同的,因此您仍然可能會錯誤地設置成員變量的值,而不是使用隱式調用retain的屬性訪問它。因此,確保您始終使用self.selectedAirport的最佳方式是爲成員變量命名與您的屬性不同的東西。

例如,在。你可以有以下實現.h文件:

NSString *_selectedAirport; 

然後封裝它的屬性裏面像下面

@property(nonatomic,retain) NSString *selectedAirport; 

和.M實現文件合成它象下面這樣:

@synthesize selectedAirport = _selectedAirport; 

這樣做以上,如果您嘗試像下面那樣訪問它

selectedAirport = [self.airportList objectAtIndex:indexPath.row]; 

然後它會導致一個錯誤,你會被提示使用self.selectedAirport。在這種情況下

而且你的dealloc方法可以有

self.selectedAirport = nil; 

[_selectedAirport release]; 
相關問題