当一堂课完成操作时发送通知?

发布于 2025-01-08 18:45:08 字数 293 浏览 0 评论 0原文

我有两个类,A 类和 B 类。A

类有一个方法

 -(void)methodA:(id)sender
{

}

,B 类有一个方法

-(void)methodB:(id)sender
{

}

现在我在 methodA 中正在进行一些工作,所以一旦完成,我想从 methodA: 发送通知到 methodB: 所以我可以根据通知进行一些操作。

那么我该怎么做呢?我是 obj-c 新手,有人可以指导我吗?

I have two classes, Class A and Class B.

Class A has a method

 -(void)methodA:(id)sender
{

}

and Class B has a method

-(void)methodB:(id)sender
{

}

Now i have some work is happening in methodA ,So once it is completed i want to send a notification from methodA: to methodB: So i can do some operation on the basis of notification.

So how can i do this? Can anybody guide me as i am new to obj-c?

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

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

发布评论

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

评论(3

十六岁半 2025-01-15 18:45:08

使用委托。来自 wiki 的简单代码:访问 http://en.wikipedia.org/wiki/Delegation_pattern

  • 委托是一个对象,其对象(通常)被调用来处理或响应特定事件或操作。
  • 您必须“告诉”接受委托的对象您希望成为委托。这是通过调用 [object setDelegate:self]; 来完成的。或设置 object.delegate = self;在你的代码中。
  • 充当委托的对象应该实现指定的委托方法。对象通常在协议中或通过类别在 NSObject 上定义方法作为默认/空方法,或两者​​兼而有之。 (正式的协议方法可能更清晰,尤其是现在 Objective-C 2.0 支持可选的协议方法。)
  • 当相关事件发生时,调用对象会检查委托是否实现了匹配的方法(使用 -respondsToSelector:)并调用该方法方法,如果有的话。然后,在将控制权返回给调用者之前,委托可以控制执行任何必须响应的操作。

Use delegate. Simple code from wiki: visit http://en.wikipedia.org/wiki/Delegation_pattern

  • A delegate is an object whose objects are (generally) called to handle or respond to specific events or actions.
  • You must "tell" an object which accepts a delegate that you want to be the delegate. This is done by calling [object setDelegate:self]; or setting object.delegate = self; in your code.
  • The object acting as the delegate should implement the specified delegate methods. The object often defines methods either in a protocol, or on NSObject via a category as default/empty methods, or both. (The formal protocol approach is probably cleaner, especially now that Objective-C 2.0 supports optional protocol methods.)
  • When a relevant event occurs, the calling object checks to see if the delegate implements the matching method (using -respondsToSelector:) and calls that method if it does. The delegate then has control to do whatever it must to respond before returning control to the caller.
落花浅忆 2025-01-15 18:45:08

在 B 类中注册一个观察者,例如:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(method:) name:@"notificationName" object:nil];

并从 A 类发布通知,例如:

[NSNotificationCenter defaultCenter] postNotificationName:@"notificationname" object:nil];

当 B 类被释放时,删除 B 类中的观察者,例如

[[NSNotificationCenter defaultCenter]removeObserver:self];

Register an Observer in Class B like :

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(method:) name:@"notificationName" object:nil];

And Post the notification from Class A like:

[NSNotificationCenter defaultCenter] postNotificationName:@"notificationname" object:nil];

Remove the Observer in your class B, when it is deallocated like

[[NSNotificationCenter defaultCenter]removeObserver:self];
腻橙味 2025-01-15 18:45:08

最简单的方法。

在 B 类的 -(id)initWithNibName: Bundle: 中,您需要添加注册 NSNotifications。

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {

         // Custom initialization        
         [[NSNotificationCenter defaultCenter] addObserver:self 
                                                  selector:@selector(methodB:) 
                                                      name:@"methodAFinished" 
                                                    object:nil];

      }

      return self;

}

然后需要在A类methodA:函数中进行如下操作。

 - (void)methodA:(id)sender {

       // Once you have completed your actions do the following

       [[NSNotificationCenter defaultCenter] postNotificationName:@"methodAFinished" object:nil];

 }

 - (void)methodB:(id)sender {

      // This will then be called in the other class, do whatever is needed in here.

 }

希望这对你有用!

另外,不要忘记,在 B 类的 -(void)viewDidDisappear:animated 函数中,您需要注销通知。

 - (void)viewDidDisappear:(BOOL)animated {

       [super viewDidDisappear:animated];
       [[NSNotificationCenter defaultCenter] removeObserver:self];

 }

这应该可以完成你所要求的。如果这不是您正在工作的内容,请添加到您的问题或在下面发表评论,我可以纠正我的答案。

Easiest way.

In Class B's -(id)initWithNibName: Bundle: you will need to add in registering for NSNotifications.

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {

         // Custom initialization        
         [[NSNotificationCenter defaultCenter] addObserver:self 
                                                  selector:@selector(methodB:) 
                                                      name:@"methodAFinished" 
                                                    object:nil];

      }

      return self;

}

And then you need to do the following in the Class A methodA: function.

 - (void)methodA:(id)sender {

       // Once you have completed your actions do the following

       [[NSNotificationCenter defaultCenter] postNotificationName:@"methodAFinished" object:nil];

 }

 - (void)methodB:(id)sender {

      // This will then be called in the other class, do whatever is needed in here.

 }

Hope that works for you!

Also, don't forget, in Class B's -(void)viewDidDisappear:animated function, you need to unregister for the notifications.

 - (void)viewDidDisappear:(BOOL)animated {

       [super viewDidDisappear:animated];
       [[NSNotificationCenter defaultCenter] removeObserver:self];

 }

That should accomplish what you're asking for. Please add to your question if this isn't what you're working or comment below and I can rectify my answer.

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