如何在两个类之间进行交集
我创建了两个类。每个类中都有 UIButton 和相关事件。我们可以点击这个按钮,条件就会改变。如果我们点击同一个类中的按钮,第二个类状态按钮应该改变。如何实现它有一个假设是通过委托来实现它,但我不太明白如何实现。
如果其中一个类按钮将其状态更改为 1,而在另一个类中此状态更改为 0,则结果应类似于图像
I created two classes. In each class there are UIButton and the related event. We can click on this button and the condition will change. If we click on the button in the same class and the second class status button should change. How to realize it there is an assumption to make it through the delegate, but I don't quite understand how.
The result should be something like the image if one of the classes button changes its status as at 1 in the other class this state changed to zero
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题,你会在每个类中声明一个委托,比如在 classA 中:
@class 子句在这里是为了避免在接口文件中导入 ClassB.h 。您确实在实现文件中导入了 ClassB.h。
您使用分配而不是保留来声明属性,因为您只想保留指向委托的指针,因此您不会分配或释放它。
您需要一种方法来接收状态更改通知。
一旦你在 classA 按钮的回调中得到了这个,你就可以调用委托上的通知方法:
当然,你需要更多的 ivars 来处理状态。
希望这有帮助。
If I understand your question correctly you would declare a delegate in each class, say in classA :
The @class clause is here to avoid having to import ClassB.h in the interface file. You do import ClassB.h in the implementation file.
You declare the property with assign and not retain as you just want to keep a pointer to the delegate, so you would not alloc or dealloc it.
You need a method to receive the status change notification.
Once you've got this in the callback of the button for classA you would call the notify method on the delegate :
Of course you'll need more ivars to handle the status.
Hope this helps.
我按如下方式解决了这个问题。
我创建一个 Objective-C 协议,其中包含一个必需的方法
接下来我创建类 A - 该类包含将与另一个类(B 类)配对的按钮
在实现文件中我添加了委托方法和反应方法
接下来我创建类 B - 这个类包含将与另一个类(A类)配对的按钮
在实现文件中我添加了委托方法和反应方法
接下来我创建实例类A和类B并绑定它们
当我点击另一个类中的一个类中的按钮时,我们调用委托方法并更改视图的状态。
I solved this problem as follows.
I create an Objective-C protocol which contain one required method
Next I create class A - this class contain button which will be paired with another class (Class B)
In implementation file I added delegate method and reaction method
Next I create class B - this class contain button which will be paired with another class (Class A)
In implementation file I added delegate method and reaction method
Next i create instance class A and class B and bind them
When I tap on the button in one class in other class we call delegate method and change state for view.