目标c,代表

发布于 2024-11-29 07:04:00 字数 555 浏览 0 评论 0原文

@protocol SomeDelegate
- (void) didSomeAction;
@end

@interface A:ViewController {
id<SomeDelegate> delegate;
}

@property (nonatomic, retain) id<SomeDelegate> delegate;

@implementation A
@synthesize delegate;

- (void)someMethod {
[delegate didSomeAction];
}

- (void)viewDidLoad {
B *b = [[B alloc] init];
}

/*=========================*/

@interface B:NSObject<SomeDelegate> {

}

@implementation B

#pragma mark - 
#pragma mark SomeDelegate methods

- (void)didSomeAction {

}

B 应该向 A 发送消息,为什么这不起作用?

@protocol SomeDelegate
- (void) didSomeAction;
@end

@interface A:ViewController {
id<SomeDelegate> delegate;
}

@property (nonatomic, retain) id<SomeDelegate> delegate;

@implementation A
@synthesize delegate;

- (void)someMethod {
[delegate didSomeAction];
}

- (void)viewDidLoad {
B *b = [[B alloc] init];
}

/*=========================*/

@interface B:NSObject<SomeDelegate> {

}

@implementation B

#pragma mark - 
#pragma mark SomeDelegate methods

- (void)didSomeAction {

}

B should send message to A, why this is not working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

情话难免假 2024-12-06 07:04:00

您需要将 b 设置为委托。

self.delegate = b;

然而,使用委托的通常方法是相反的:

SomeClass* obj = [[SomeClass alloc] init];
obj.delegate = self;

请注意,委托不是 Objective-C 的一个功能。这只是一种设计模式!
http://en.wikipedia.org/wiki/Delegation_pattern

You need to set b as delegate.

self.delegate = b;

However the usual way to use delegates is the other way round:

SomeClass* obj = [[SomeClass alloc] init];
obj.delegate = self;

Note that Delegation is not a feature of Objective-C. It is only a design pattern!
http://en.wikipedia.org/wiki/Delegation_pattern

情深如许 2024-12-06 07:04:00

如果我正确理解你想要做什么,这应该是正确的:

 -(void)someMethod {
    [delegate didSomeAction];
 }

 -(void)viewDidLoad {
    delegate = [[B alloc] init];
 }

否则,当你调用 [delegate didSomeAction]; 时,delegate 为零并且消息被忽略。

If I understand correctly what you are trying to do, this should be correct:

 -(void)someMethod {
    [delegate didSomeAction];
 }

 -(void)viewDidLoad {
    delegate = [[B alloc] init];
 }

otherwise, when you call [delegate didSomeAction];, delegate is nil and the message is ignored.

人生戏 2024-12-06 07:04:00

对象通常不会自己创建它们的委托。相反,B 应该创建 A 的实例并将其自身设置为该对象的委托:

@implementation B

- (void)awakeFromNib {
    self.a = [[[A alloc] init] autorelease];
    self.a.delegate = self;
}

@end

Objects don’t usually create their delegates themselves. Instead B should create an instance of A and set itself as the delegate of that object:

@implementation B

- (void)awakeFromNib {
    self.a = [[[A alloc] init] autorelease];
    self.a.delegate = self;
}

@end
怎樣才叫好 2024-12-06 07:04:00

如果您只想向其他类发送简单消息,可以

在 B 类文件中

- (void)someMethod {
     [[NSNotificationCenter defaultCenter] postNotificationName:@"NofifyMe" object:self userInfo:nil];
}

使用 NSNotification 在A 类文件中

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSomeAction:) name:@"NofifyMe" object:nil];
- (void)didSomeAction {

}

因此,每当您的 someMethod 调用时,您的 B 类都会向您的 A 类发送一条消息以调用 didSomeAction。并在 dealloc 中删除观察者

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

If you just want to send the simple message to other class, can use NSNotification

In B class file

- (void)someMethod {
     [[NSNotificationCenter defaultCenter] postNotificationName:@"NofifyMe" object:self userInfo:nil];
}

In A class file

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSomeAction:) name:@"NofifyMe" object:nil];
- (void)didSomeAction {

}

So whenever your someMethod will call, then your B class will just send a message to your class A to invoke didSomeAction. and in dealloc remove the observer

-(void)dealloc{
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [super dealloc];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文