如何在两个类之间进行交集

发布于 2024-12-10 02:39:00 字数 228 浏览 0 评论 0原文

我创建了两个类。每个类中都有 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.enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以畏孤独 2024-12-17 02:39:00

如果我正确理解你的问题,你会在每个类中声明一个委托,比如在 classA 中:

@class ClassB;

interface classA: UIbutton {
    ClassB* delegate;
    ...
}
@property (nonatomic, assign) ClassB* delegate;
- (void) notifystatuschange:(int) status;

@class 子句在这里是为了避免在接口文件中导入 ClassB.h 。您确实在实现文件中导入了 ClassB.h。
您使用分配而不是保留来声明属性,因为您只想保留指向委托的指针,因此您不会分配或释放它。
您需要一种方法来接收状态更改通知。

一旦你在 classA 按钮的回调中得到了这个,你就可以调用委托上的通知方法:

-(void) classAcallbak:(ClassA*) sender {
    [sender.delegate notifystatuschange:1];
    ...
}

当然,你需要更多的 ivars 来处理状态。

希望这有帮助。

If I understand your question correctly you would declare a delegate in each class, say in classA :

@class ClassB;

interface classA: UIbutton {
    ClassB* delegate;
    ...
}
@property (nonatomic, assign) ClassB* delegate;
- (void) notifystatuschange:(int) status;

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 :

-(void) classAcallbak:(ClassA*) sender {
    [sender.delegate notifystatuschange:1];
    ...
}

Of course you'll need more ivars to handle the status.

Hope this helps.

淤浪 2024-12-17 02:39:00

我按如下方式解决了这个问题。

我创建一个 Objective-C 协议,其中包含一个必需的方法

@protocol MyProtocolForState <NSObject>

- (void)changeStateButton;

接下来我创建类 A - 该类包含将与另一个类(B 类)配对的按钮

#import "MyProtocolForState.h"

@interface A : UIViewController <MyProtocolForState>{
    id<MyProtocolForState.h> delegate;
}
@property (nonatomic, assign)id<MyProtocolForState.h> delegate;
- (IBAction)touchButton:(id)sender;

在实现文件中我添加了委托方法和反应方法

- (IBAction)touchButton:(id)sender {
    [self.view setBackgroundColor:[UIColor redColor]];
    [delegate changeMethod];
}

- (void)changeMethod{
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

接下来我创建类 B - 这个类包含将与另一个类(A类)配对的按钮

#import "MyProtocolForState.h"

@interface B : UIViewController <MyProtocolForState>{
    id<MyProtocolForState.h> delegate;
}
@property (nonatomic, assign)id<MyProtocolForState.h> delegate;
- (IBAction)touchButton:(id)sender;

在实现文件中我添加了委托方法和反应方法

- (IBAction)touchButton:(id)sender {
    [self.view setBackgroundColor:[UIColor redColor]];
    [delegate changeMethod];
}

- (void)changeMethod{
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

接下来我创建实例类A和类B并绑定它们

    A * a = [[A alloc] init];
    [a.view setFrame:CGRectMake(50, 50, 100, 100)];
    B * b = [[B alloc] init];
    [b.view setFrame:CGRectMake(200, 50, 100, 100)];
    a.delegate = b;
    b.delegate = a;
    [self.window addSubview:a.view];
    [self.window addSubview:b.view];

当我点击另一个类中的一个类中的按钮时,我们调用委托方法并更改视图的状态。

I solved this problem as follows.

I create an Objective-C protocol which contain one required method

@protocol MyProtocolForState <NSObject>

- (void)changeStateButton;

Next I create class A - this class contain button which will be paired with another class (Class B)

#import "MyProtocolForState.h"

@interface A : UIViewController <MyProtocolForState>{
    id<MyProtocolForState.h> delegate;
}
@property (nonatomic, assign)id<MyProtocolForState.h> delegate;
- (IBAction)touchButton:(id)sender;

In implementation file I added delegate method and reaction method

- (IBAction)touchButton:(id)sender {
    [self.view setBackgroundColor:[UIColor redColor]];
    [delegate changeMethod];
}

- (void)changeMethod{
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

Next I create class B - this class contain button which will be paired with another class (Class A)

#import "MyProtocolForState.h"

@interface B : UIViewController <MyProtocolForState>{
    id<MyProtocolForState.h> delegate;
}
@property (nonatomic, assign)id<MyProtocolForState.h> delegate;
- (IBAction)touchButton:(id)sender;

In implementation file I added delegate method and reaction method

- (IBAction)touchButton:(id)sender {
    [self.view setBackgroundColor:[UIColor redColor]];
    [delegate changeMethod];
}

- (void)changeMethod{
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

Next i create instance class A and class B and bind them

    A * a = [[A alloc] init];
    [a.view setFrame:CGRectMake(50, 50, 100, 100)];
    B * b = [[B alloc] init];
    [b.view setFrame:CGRectMake(200, 50, 100, 100)];
    a.delegate = b;
    b.delegate = a;
    [self.window addSubview:a.view];
    [self.window addSubview:b.view];

When I tap on the button in one class in other class we call delegate method and change state for view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文