gpt4 book ai didi

iphone - 类中两个独立的委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-29 03:41:52 25 4
gpt4 key购买 nike

我在一个类中有两个独立的委托(delegate)方法。

    - (void)delegateMethod1:(id)data {
self.data = data;
}

- (void)delegateMethod2 {
[someClass sendData:self.data];
}

现在,有时这工作正常,但其他时候,delegateMethod2 在 delegateMethod1 之前被调用。

我需要知道如何优雅地管理它,以便仅当 delegateMethod1 和 delegateMethod2 都被调用时才调用行:[someClass sendData:self.data];

我知道我可以通过使用变量在每个委托(delegate)调用上设置一些内容来做到这一点,但必须有一种优雅的方法来做到这一点。

有什么帮助吗?

最佳答案

记住哪个委托(delegate)被调用对我来说似乎是最简单、最干净的解决方案。但是您可以通过将检查移至单独的方法来使其对称,这样首先调用哪个委托(delegate)并不重要:

- (void)checkIfDataCanBeSent {
if (self.method1called && self.method2called) {
[someClass sendData:self.data];
}
}

- (void)delegateMethod1:(id)data {
self.method1called = YES;
// ...
[self checkIfDataCanBeSent];
}

- (void)delegateMethod2 {
self.method2called = YES;
// ...
[self checkIfDataCanBeSent];
}

(我假设所有委托(delegate)方法都在主线程上调用,否则必须添加一些同步。)

关于iphone - 类中两个独立的委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18310278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com