0

Objective C の 3 つのビュー間で変数を渡すことができません。2 つしかない限り、あるクラスから別のクラスにデータを渡すことができますが、同じデリゲート メソッドにアクセスする必要がある別のビューを追加すると、できません。それで。

説明してみましょう:

View1 は、View2 で宣言されたデリゲート メソッドにアクセスします。ただし、View3 という別のビューを追加し、View2 のデリゲート メソッドにアクセスする必要がある場合、アクセスできません。すべてを正しく宣言し、デリゲート メソッドを参照できますが、それでも View3 でその参照を入力できません。

4

2 に答える 2

0

2 つのオブジェクトの両方が 3 番目のオブジェクトのデリゲートになることはできません。それはあなたの問題ですか?その場合は、メッセージを送信するために NSNotification を使用することを検討してください。複数のオブジェクトが通知をサブスクライブできます。

于 2012-08-03T11:31:02.733 に答える
0

1 クラスから 3 クラスにデータを渡したい場合は、NSNotification を使用することをお勧めします。このように使えます。

最初の受信クラスで:

@implementation TestClass1

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (!self) return nil;

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

    return self;
}

- (void) receiveNotification1:(NSNotification *) notification
{
    NSLog(@"receive 1");
}

@end

2番目の受信クラスで:

@implementation TestClass2

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (!self) return nil;

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

    return self;
}

- (void) receiveNotification2:(NSNotification *) notification
{
    NSLog(@"receive 2");
}

@end

3番目の受信クラスで:

@implementation TestClass3

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (!self) return nil;

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

    return self;
}

- (void) receiveNotification3:(NSNotification *) notification
{
    NSLog(@"receive 3");
}

@end

投稿クラスで:

- (void) yourMethod
{

    [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:self];

}
于 2012-08-03T13:21:59.497 に答える