2009-12-30 70 views
0

如何保存實例以便我可以在另一種方法中使用它? 這是我的代碼:在多個方法中使用實例變量

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint  touchedStart; 

    // find position of the touch 
    touchedStart = [[touches anyObject] locationInView:self]; 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint  touchedEnd; 

    // find position of the touch 
    touchedEnd = [[touches anyObject] locationInView:self]; 

} 

我希望能夠使用touchedStart變量在touchesEnded方法。我將如何做到這一點?

回答

1

只要讓它成爲類本身的成員變量 - 那麼另一種方法就可以訪問它。

+0

什麼是成員變量? – Joshua 2009-12-30 18:49:06

+0

@Joshua - 這是一個變量,它是類的成員(也稱爲實例變量),而不是現在你所擁有的「局部變量」。 – 2009-12-30 18:56:02

+0

是的,我知道你的意思,我只是有點困惑。謝謝! – Joshua 2009-12-31 06:44:55