当一堂课完成操作时发送通知?
我有两个类,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用委托。来自 wiki 的简单代码:访问 http://en.wikipedia.org/wiki/Delegation_pattern
Use delegate. Simple code from wiki: visit http://en.wikipedia.org/wiki/Delegation_pattern
在 B 类中注册一个观察者,例如:
并从 A 类发布通知,例如:
当 B 类被释放时,删除 B 类中的观察者,例如
Register an Observer in Class B like :
And Post the notification from Class A like:
Remove the Observer in your class B, when it is deallocated like
最简单的方法。
在 B 类的
-(id)initWithNibName: Bundle:
中,您需要添加注册 NSNotifications。然后需要在A类methodA:函数中进行如下操作。
希望这对你有用!
另外,不要忘记,在 B 类的
-(void)viewDidDisappear:animated
函数中,您需要注销通知。这应该可以完成你所要求的。如果这不是您正在工作的内容,请添加到您的问题或在下面发表评论,我可以纠正我的答案。
Easiest way.
In Class B's
-(id)initWithNibName: Bundle:
you will need to add in registering for NSNotifications.And then you need to do the following in the Class A methodA: function.
Hope that works for you!
Also, don't forget, in Class B's
-(void)viewDidDisappear:animated
function, you need to unregister for the notifications.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.