2012-02-29 5 views
1

オブジェクトCのパラメータとしてオブジェクトを渡す方法が不思議でした。ご覧のとおり、Obj1 * myObj1はbtnIncrementObj1メソッドのスコープ外です。どのように私はそれを範囲に入れますか?クラスのインスタンスを静的にする方法があると思っています。Objective Cのクラスのグローバルインスタンス

ご覧のとおり、ビューがロードされたときではなく、myObj1をボタン・プレスでインスタンス化するだけです。

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

OK、ここに私の答えです。これはLancによって与えられたものに拡張されます。

まず、myObj1のスコープを決定する必要があります。クラスのインスタンスごとに1つ、またはアプリケーション全体に対して1つだけを必要としますか?クラスのインスタンスごとの場合は、インスタンス変数を作成し、必要に応じてインスタンス変数を作成するプロパティを設定します。例えば

@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]]; 
} 

あなたはシングルトン(プログラムごとに、すなわち、1つのオブジェクトのみを)必要な場合は、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

1

クラス全体の変数にアクセスする場合は、クラスメンバーとして作成します。このような他のクラスからアクセスすることができますあなたにここから

static Obj1* _myObj1 = nil; 

:あなたは静的オブジェクトとして作成することができます右ここ

@implementation 

後にオブジェクトを作成する必要があり

+0

をオブジェクトを解放する必要がありますか? – iggy2012

0

[Obj1 myObj1].property 
+0

私の目的がボタンを押すだけでインスタンス化することである場合でも、これを行うのは一般的なプラクティスですか? – iggy2012

+0

はい。これは、オブジェクトを共有するほとんどすべてのアプリケーションで行われます。あなたの問題を解決した場合は、ベストアンサーとして評価してください。 – wizH

+0

@ iggy2012:これは一般的ではありません。上の例では、おそらくクラスメンバーを作成したいと思うでしょう。グローバルオブジェクトはほとんど常に悪い考えです。 – JeremyP

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]; 
} 
関連する問題