2009-04-01 13 views
3

次のシナリオを取り除くためのヒントを提供します。1つのViewControllerから別のViewControllerにオブジェクトにアクセスする方法

説明:

私は2つのviewControllersすなわちViewController1ViewController2を持っているので、obiviously我々はViewController2.mViewController1.hViewController1.mViewController2.hを持っています。 は、今私は

@synthesize string1; 

と同様にViewController1.mViewController1.h

NSString *string1; 

を宣言し、プロパティ

@property(nonatomic,retain) NSString *string1; 

として、それを宣言し、でそれを合成しましたViewController1.m、i set

[email protected]"Hello Every One"; 

として文字列1の値は、同様にIはViewController2.h

NSString *string2; 

を宣言し、プロパティ

@property(nonatomic,retain)NSString *string2; 

として宣言さViewController2.mとしてでそれを合成します

@synthesize string2; 

私は、私はそれをどのように行うことができ、(ViewController2.mに)string2に(ViewController1.mに)string1値を設定したい場合は?

答えて

3

これは、string1を設定するコードがどこで実行されているかによって異なります。それが両方のView Controllerオブジェクトへのアクセスを持ついくつかの外部クラスにあれば、それは簡単です。あなたはViewController1オブジェクトVC1、およびViewController2オブジェクトVC2を持っている場合は、あなたがすべてです:

[vc1 setString1:[vc2 string2]]; 

あなたはViewController2内で実行コードから文字列1を設定したい場合は、あなたが通知メカニズムを使用します。あなたは、文字列に変更したいとき

-(void)aChangeStringMethod:(NSNotification)notification{ 
    string1 = [((ViewController2 *)[notification object]) string2]; 
} 

その後、ViewController2に、::ViewController1の初期化ルーチンでは、あなたが入れ:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aChangeStringMethod:) name:@"anyStringJustMakeItUnique" object:nil]; 

と定義を

[[NSNotificationCenter defaultCenter] postNotificationName:@"anyStringJustMakeItUnique" withObject:self]; 

を同じ技術が使用されていますvc2にアクセスできるがvc1にアクセスできない第3のクラスから文字列を変更しているとき。 ViewController1コードは上記と同じである、とあなたは、文字列を変更したいとき:あなたは(あなたがオブジェクトへのアクセスを持っていないと仮定しViewController1内から文字列を変更したい場合は

[[NSNotificationCenter defaultCenter] postNotificationName:@"anyStringJustMakeItUnique" withObject:vc2]; 

トリッキーな部分がありますvc2)。あなたは、これはあまりにも複雑であるか、原因と考えられる場合は

[[NSNotificationCenter defaultCenter] postNotificationName:@"anotherNotificationName" withObject:nil]; 

:上記1、およびまた、ViewController2用:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(launchTheOtherNotificationMethod:) name:@"anotherNotificationName" object:nil]; 

-(void)launchTheOtherNotificationMethod:(NSNotification)notification{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"anyStringJustMakeItUnique" withObject:self]; 
} 

その後、あなたは文字列を変更したいあなたは、2つの通知を使用する必要がありますあまりにも多くのオーバーヘッド、簡単なソリューションは、ViewController1とViewController2のフィールドとして、相互にポインタを持つことです。その後、ViewController1内:

string1 = [myVC2 string2]; 

そして、あなたはこれらのフィールドのプロパティを作成する場合は、外部から:

[vc1 setString1:[[vc1 myVC2] string2]]; 

とさえ:

[[vc2 myVC1] setString1:[vc2 string2]]; 
0

2つの文字列を含み、両方のコントローラで認識されるモデルオブジェクトを使用できます。

もう1つが文字列の値を更新するたびに各コントローラに通知する場合は、notification mechanismを使用できます。これにより、モデルは他のオブジェクトにその変更を知らせ、これらのオブジェクトから独立した状態を保つことができます。

1

viewControllersは、スタックされているので、どちらか最後に呼ばれたものがスタックの上にあり、呼び出されたものがその親です。

[[self parentViewController] setString1:string2] 

:D

のでviewController1が最初に呼び出され、その後、viewController2がその内から呼び出されます、そして、あなたはviewController2.m内に持っているすべてがあると仮定すると
関連する問題