如何在 Objective-C 中创建 NSNotification 的对象? [英] How to create an object of NSNotification in Objective-C?

查看:78
本文介绍了如何在 Objective-C 中创建 NSNotification 的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 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: or notificationWithName:object:userInfo:. However, you don’t usually create your own notifications directly. The NSNotificationCenter methods postNotificationName:object: and postNotificationName:object:userInfo: allow you to conveniently post a notification without creating it first.

这篇关于如何在 Objective-C 中创建 NSNotification 的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆