2011-11-14 41 views
0

簡單的問題,但這給了我一個錯誤,我似乎無法解決它。調用對象的方法給SIGBART

在我對象(的UIViewController)我已經宣佈在.h

​​

的方法,並在.m

-(void)setCurrentLoc:(CLLocation *)loc 
{ 
    currentLocation = loc; 
} 

都很好。但是,這是我如何啓動這一對象(正常工作),但是當我打電話setCurrentLoc:它給SIGBART

.h

IBOutlet VectorViewController *vectorView; 

.m

vectorView = [[VectorViewController alloc] init]; 
... 
//In another method I call: 
[vectorView setCurrentLoc:location]; // Error/SIGBART line 

有什麼我錯過了什麼?也許不會讓它成爲全球或什麼?

錯誤:

-[UIView setCurrentLoc:]: unrecognized selector sent to instance 0x841cab0

+0

您是否聲明併合成了currentLocation?也許你應該使用self.currentLocation = loc; – alinoz

+0

是的,我做過了,但事實並非如此,這給了錯誤......它調用了方法。 – LouwHopley

回答

1

-[UIView setCurrentLoc:]: unrecognized selector sent to instance 0x841cab0

這意味着你將消息發送到一個不再在你VectorViewController指向一個指針,但現在在UIView指向。這通常發生在對象被自動釋放或釋放並且內存被重新使用時。

請仔細檢查在創建vectorView和在另一個方法中再次調用vectorView之間會發生什麼。您似乎直接設置和訪問實例變量 - 您應該可以通過屬性訪問它(很難確定是否使用ARC),並確保正確聲明這些屬性。

+0

發生這種情況是因爲它與vectorView被初始化之前發生的locationUpdate混淆,所以我把它放在locationUpdate中(用if來檢查它是否已經存在) - 現在工作正常。謝謝 – LouwHopley

0

根據您所編寫的代碼,你並不需要提供setCurrentLoc方法,用一個屬性,而不是和合成代替。

在VectorViewController類

,使用方法:

@property (nonatomic, retain) IBOutlet CLLocation * currentLocation; 
在你的@implementation

,添加:

@synthesize currentLocation; 

,並在你的dealloc方法,添加:

self.currentLocation = nil; 

保留現有按原樣調用setCurrentLocation。

+0

我知道這一點。但事實並非如此......這是因爲我在'vectorView'存在之前以某種方式調用它。 – LouwHopley