2012-02-29 28 views
1

我想知道如何將對象作爲參數傳遞給目標C.如您所見,在我的代碼中,Obj1 * myObj1超出了btnIncrementObj1方法的作用域。我如何把它放在範圍內?我在想,有一種方法可以將類的實例設置爲靜態。您可以看到,我只希望myObj1在按下按鈕時實例化,而不是在視圖加載時實例化。目標C中的類的全局實例C

有沒有辦法讓Obj1變成靜態的,還是給它一個全局範圍?

- (IBAction)btnCreateObj1:(UIButton *)sender 
{ 
    Obj1 * myObj1 = [[Obj1 alloc] init]; 

    int intVal = [self.textField.text intValue]; 

    [myObj1 increment:intVal]; 

    [myObj1 restring:@"orig string 1"]; 

    NSString * newLabel = [self.labelObject1.text stringByAppendingFormat:@"value:%d string:%@",myObj1.value,myObj1.someString]; 

    self.labelObject1.text = newLabel; 
} 

- (IBAction)btnIncrementObj1:(UIButton *)sender 
{ 
    //-I want to increment myObj1.value by [self.textField.text intValue] 
} 

回答

1

好的,這是我的回答。這擴大了Lanc給出的。

首先,您需要確定myObj1的範圍應該是什麼。你想每個實例的類或整個應用程序只有一個。如果它是每個類的實例,則創建一個實例變量並擁有一個按需創建它的屬性。例如

@interface MyViewController : UIViewController 

@property (nonatomic, readonly, retain) Obj1* myObj1; 

// other stuff 

@end 

@implementation MyViewController 
{ 
@private 
    Obj1* myObj1; 
} 

-(Obj1*) myObj1 
{ 
    @synchronized(self) // if you know you are single threaded you can omit the @synchronized block 
    { 
     if (myObj1 == nil) 
     { 
      myObj1 = [[Obj1 alloc] init]; 
     } 
    } 
    return myObj1; 
} 

- (IBAction)btnCreateObj1:(UIButton *)sender 
{ 
    [[self myObj1] increment:intVal]; 

    [[self myObj1] restring:@"orig string 1"]; 

    NSString * newLabel = [self.labelObject1.text stringByAppendingFormat:@"value:%d string:%@",myObj1.value,myObj1.someString]; 

    self.labelObject1.text = newLabel; 
} 

- (IBAction)btnIncrementObj1:(UIButton *)sender 
{ 
    [[self myObj1] increment: [self.textField.text intValue]]; 
} 

如果你需要一個單身(即僅針對每個程序對象),你可以使用一個靜態變量按照wizH的答案,但我更喜歡使用的方法來訪問它。所以,下面的工作:

@interface MyViewController : UIViewController 

@property (nonatomic, readonly, retain) Obj1* myObj1; 

// other stuff 

@end 

@implementation MyViewController 

-(Obj1*) myObj1 
{ 
    static Obj1* myObj1 = nil; // instance var moved to be a static variable 
    @synchronized([MyViewController class) // if you know you are single threaded you can omit the @synchronized block 
    { 
     if (myObj1 == nil) 
     { 
      myObj1 = [[Obj1 alloc] init]; 
     } 
    } 
    return myObj1; 
} 

- (IBAction)btnCreateObj1:(UIButton *)sender 
{ 
    [[self myObj1] increment:intVal]; 

    [[self myObj1] restring:@"orig string 1"]; 

    NSString * newLabel = [self.labelObject1.text stringByAppendingFormat:@"value:%d string:%@",myObj1.value,myObj1.someString]; 

    self.labelObject1.text = newLabel; 
} 

- (IBAction)btnIncrementObj1:(UIButton *)sender 
{ 
    [[self myObj1] increment: [self.textField.text intValue]]; 
} 

注意,已經改變的唯一事情是如何爲類的API是滿意的方式。沒有使用API​​的代碼必須改變。

+0

非常感謝你。令人驚訝的偉大答案。 – iggy2012 2012-02-29 21:46:09

1

如果您想在整個類中訪問變量,請將其設置爲類成員。

+0

將這個代碼是什麼對象? – iggy2012 2012-02-29 10:22:51

0

您應該創建對象後,右側的

@implementation 

在這裏,你可以創建它作爲一個靜態對象:

static Obj1* _myObj1 = nil; 

從這裏,你可以從其他類訪問它像這樣:

[Obj1 myObj1].property 
+0

即使我的目標是僅在按下按鈕時進行實例化,這是否是常見做法? – iggy2012 2012-02-29 09:51:56

+0

是的。這幾乎在我所有共享對象的應用程序中完成。如果它解決了你的問題,請評價爲最佳答案。 – wizH 2012-02-29 10:03:14

+0

@ iggy2012:不,這不是一種常見的做法。您可能想在上面的示例中創建一個類成員。全局對象幾乎總是一個壞主意。 – JeremyP 2012-02-29 10:08:07

0

您的.h文件應導入您想要保留的對象並在該類中創建該對象的成員。 它看起來像這樣。

#import "Obj1.h"; 

@interface MyViewController : UIViewController{ 
    Obj1 * myObj1; 
} 

我不會在IBAction方法中初始化一個類變量,但您可以這樣做。 在創建的按鈕中,您可以保留相同的代碼。但在其他方法中,只需檢查是否創建了myObj1。

- (IBAction)btnIncrementObj1:(UIButton *)sender 
{ 
    if(myObj1){ 
     //Handle the increment. 
    }else{ 
     //Handle what to do if you haven't clicked the other button before this one. 
    } 
} 

而對於好的內存管理應該釋放的dealloc

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