【问题标题】:Passing data from 3 classes to one another in Objective C在Objective C中将数据从3个类传递给另一个类
【发布时间】:2012-08-01 10:39:16
【问题描述】:

我无法在 Objective C 中的 3 个视图之间传递变量。我可以将数据从一个类传递到另一个类,只要只有 2 个,但如果我添加另一个需要访问相同委托方法的视图,我会无法这样做。

让我试着解释一下:

View1 访问在 View2 中声明的委托方法。但是,如果我添加另一个名为 View3 的视图并需要访问 View2 中的委托方法,我不能。我正确声明了所有内容,并且能够引用委托方法,但我仍然无法在 View3 中输入该引用。

【问题讨论】:

  • 这很简单:当你创建一个对象时,在对象中设置属性来处理你需要的数据。或者保留一个指向创建对象的指针,以便其他对象可以在以后设置属性。这是一个简单的寻址问题。

标签: ios objective-c xcode methods delegates


【解决方案1】:

两个对象不能同时是第三个对象的委托。那是你的问题吗?如果是这样,请考虑使用 NSNotification 来发送消息:多个对象可以订阅通知。

【讨论】:

    【解决方案2】:

    如果你想将数据从 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
    

    在第二个接收类中:

    @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
    

    在第三个接收类中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 2019-03-09
      • 2017-08-13
      • 2015-07-09
      • 1970-01-01
      相关资源
      最近更新 更多