iOS 通知机制 Notifications (二)

了解如何在应用程序内或跨应用注册并接收本地与分布式通知,包括通知的注册、取消注册及对象观察过程,以及如何处理通知的传递和暂停机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Registering for a Notification

You can register for notifications from within your own application or other applications. See “Registering forLocal Notifications” (page 12) for the former and “Registering for Distributed Notifications” (page 13) for thelatter. To unregister for a notification, which must be done when your object is deallocated, see “Unregisteringan Observer” (page 15). 


注册一个通知

你可以在你应用内注册通知,也可以在其他应用里。对于前者或者后者你可以参考相应的文档。当你的对象被释放时,你才可以解除注册通知,你可以参考相应的文档。


Registering for Local Notifications

注册本地通知

You register an object to receive a notification by invoking the notification center method addObserver:selector:name:object:, specifying the observer, the message the notification center should send to the observer, the name of the notification it wants to receive, and about which object. You don't need to specify both the name and the object. If you specify only an object, the observer will receive all notifications containing that object. If you specify only a notification name, the observer will receive that notification every time it’s posted, regardless of the object associated with it.


你可以调用通知中心的方法 addObserver:selector:name:object:,指定观察者,通知中心应该发给观察者的消息,通知的名称,以及关于哪个对象。你不需要同时指定名字和对象。如果你只指定一个对象,观察者将会接收所有的包含那个对象的通知。如果你只指定一个通知名称,那么观察者每次都会接收通知,无视与他相关的对象。

It is possible for an observer to register to receive more than one message for the same notification. In such a case, the observer will receive all messages it is registered to receive for the notification, but the order in which it receives them can not be determined.

有可能,对于同一个通知,观察者会注册接收不只一个消息。在这种情况下,观察者将接收所有的消息,但是无法决定接收消息的顺序。

If you later decide an observer no longer needs to receive notifications (for example, if the observer is being deallocated), you can remove the observer from the notification center’s list of observers with the methods removeObserver: or removeObserver:name:object:.

如果你后来不想让观察者继续接收通知了(比如,观察者被释放了),你可以通过 removeObserver: or removeObserver:name:object:.把观察者从通知中心列表中移走

Normally, you register objects with the process’s default notification center. You obtain the default object using the defaultCenter class method.

通常,你使用 进程的默认通知中心注册对象。你用defaultCenter类方法获取默认对象。


As an example of using the notification center to receive notifications, suppose you want to perform an operation any time a window becomes main (for example, if you’re implementing a controller for an inspector panel). You would register your client object as an observer as shown in the following example:

下面是个例子

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(aWindowBecameMain:)
    name:NSWindowDidBecomeMainNotification object:nil];

2009-08-18 | Copyright © 2009 Apple Inc. All Rights Reserved.12

Registering for a Notification

Registering for Distributed Notifications

By passing nil as the object to observe, the client object (self) is notified when any object posts aNSWindowDidBecomeMainNotification notification.

因为object参数被传递了nil,所以任何对象抛出这个通知时客户对象(self)将会被通知

When window becomes main, it posts an NSWindowDidBecomeMainNotification to the notification center.The notification center notifies all observers who are interested in the notification by invoking the method they specified in the selector argument of addObserver:selector:name:object:. In the case of our example observer, the selector is aWindowBecameMain:. The aWindowBecameMain: method might have the following implementation:

- (void)aWindowBecameMain:(NSNotification *)notification {
    NSWindow *theWindow = [notification object];
    MyDocument = (MyDocument *)[[theWindow windowController] document];
    // Retrieve information about the document and update the panel.
}

The NSWindow objects don’t need to know anything about your inspector panel. 


Registering for Distributed Notifications

An object registers itself to receive a notification by sending the addObserver:selector:name:object:suspensionBehavior: method to an NSDistributedNotificationCenter object, specifying the message the notification should send, the name of the notification it wants to receive, the identifying string to match (the object argument), and the behavior to follow if notification delivery is suspended.

Because the posting object and the observer may be in different processes, notifications can’t contain pointers to arbitrary objects. Therefore, the NSDistributedNotificationCenter class requires notifications to use an NSString object as the object argument. Notification matching is done based on this string, rather than an object pointer. You should check the documentation of the object posting the notification to see what it uses as its identifying string.

When a process is no longer interested in receiving notifications immediately, it may suspend notification delivery. This is often done when the application is hidden, or is put into the background. (The NSApplication object automatically suspends delivery when the application is not active.) The suspensionBehavior argument in the addObserver method identifies how arriving notifications should be handled while delivery is suspended. There are four different types of suspension behavior, each useful in different circumstances.

Suspension Behavior

Description

NSNotificationSuspensionBehaviorDrop

The server does not queue any notifications with this name and object until it receives the setSuspended:NO message.

NSNotificationSuspensionBehaviorCoalesce

The server queues only the last notification of the specified name and object; earlier notifications are dropped. In cover methods for which suspension behavior is not an explicit argument,NSNotificationSuspensionBehaviorCoalesce is the default.

NSNotificationSuspensionBehaviorHold

The server holds all matching notifications until the queue has been filled (queue size determined by the server) at which point the server may flush queued notifications.

NSNotificationSuspensionBehaviorDeliverImmediately

The server delivers notifications matching this registration irrespective of whether it has received the setSuspended:YES message. When a notification with this suspension behavior is matched, it has the effect of first flushing any queued notifications. The effect is as if the server received setSuspended:NO while the application is suspended, followed by the notification in question being delivered, followed by a transition back to the previous suspended or unsuspended state.

You suspend notifications by sending setSuspended:YES to the distributed notification center. While notifications are suspended, the notification server handles notifications destined for the process that suspended notification delivery according to the suspension behavior specified by the observers when they registered to receive notifications. When the process resumes notification delivery, all queued notifications are delivered immediately. In applications using Application Kit, the NSApplication object automatically suspends notification delivery when the application is not active.

Note that a notification destined for an observer that registered with NSNotificationSuspensionBehaviorDeliverImmediately, automatically flushes the queue as it is delivered, causing all queued notifications to be delivered at that time as well.

The suspended state can be overridden by the poster of a notification. If the notification is urgent, such as a warning of a server being shut down, the poster can force the notification to be delivered immediately to all observers by posting the notification with theNSDistributedNotificationCenter postNotificationName:object:userInfo:deliverImmediately: method with thedeliverImmediately argument YES.


这一块暂时不研究了,我暂时也用不到

Unregistering an Observer

解除注册通知

Before an object that is observing notifications is deallocated, it must tell the notification center to stop sending it notifications. Otherwise, the next notification gets sent to a nonexistent object and the program crashes. You can send the following message to completely remove an object as an observer of local notifications, regardless of how many objects and notifications for which it registered itself:

[[NSNotificationCenter defaultCenter] removeObserver:self];

For observers of distributed notifications send:

[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];

Use the more specific removeObserver... methods that specify the notification name and observed object to selectively unregister an object for particular notifications.



python+opencv简谱识别音频生成系统源码含GUI界面+详细运行教程+数据 一、项目简介 提取简谱中的音乐信息,依据识别到的信息生成midi文件。 Extract music information from musical scores and generate a midi file according to it. 、项目运行环境 python=3.11.1 第三方库依赖 opencv-python=4.7.0.68 numpy=1.24.1 可以使用命令 pip install -r requirements.txt 来安装所需的第三方库。 三、项目运行步骤 3.1 命令行运行 运行main.py。 输入简谱路径:支持图片或文件夹,相对路径或绝对路径都可以。 输入简谱主音:它通常在第一页的左上角“1=”之后。 输入简谱速度:即每分钟拍数,同在左上角。 选择是否输出程序中间提示信息:请输入Y或N(不区分大小写,下同)。 选择匹配精度:请输入L或M或H,对应低/中/高精度,一般而言输入L即可。 选择使用的线程数:一般与CPU核数相同即可。虽然python的线程不是真正的多线程,但仍能起到加速作用。 估算字符上下间距:这与简谱中符号的密集程度有关,一般来说纵向符号越稀疏,这个值需要设置得越大,范围通常在1.0-2.5。 值化算法:使用全局阈值则跳过该选项即可,或者也可输入OTSU、采用大津值化算法。 设置全局阈值:如果上面选择全局阈值则需要手动设置全局阈值,对于.\test.txt中所提样例,使用全局阈值并在后面设置为160即可。 手动调整中间结果:若输入Y/y,则在识别简谱后会暂停代码,并生成一份txt文件,在其中展示识别结果,此时用户可以通过修改这份txt文件来更正识别结果。 如果选择文件夹的话,还可以选择所选文件夹中不需要识别的文件以排除干扰
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值