如何在 Objective-C 中创建 NSNotification 的对象? [英] How to create an object of NSNotification in Objective-C?
问题描述
我想创建一个 NSNotification 对象,比如:
I want to create an object of NSNotification as say:
NSNotification *obj=[[NSNotification alloc]init];
但是当我这样创建时,我得到一个异常,因为NSConcreteNotification init: is not allowed".我该如何解决这个问题?
but when i create like this i get an exception as 'NSConcreteNotification init: is not allowed'. How should i solve this problem?
推荐答案
来自 NSNotification 文档:
您可以使用类方法创建通知对象 notificationWithName:object:
或 NotificationWithName:object:userInfo:
.但是,您通常不会直接创建自己的通知.NSNotificationCenter 方法 postNotificationName:object:
和 postNotificationName:object:userInfo:
让您无需先创建即可方便地发布通知.
You can create a notification object with the class methods
notificationWithName:object:
ornotificationWithName:object:userInfo:
. However, you don’t usually create your own notifications directly. The NSNotificationCenter methodspostNotificationName:object:
andpostNotificationName:object:userInfo:
allow you to conveniently post a notification without creating it first.
这篇关于如何在 Objective-C 中创建 NSNotification 的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!