これは、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]];