一个关于NSNotification......的快速问题如果我NSNotifications在一个方法中发布两个,并且它们被不同的对象观察到,选择器方法的执行顺序是什么?
例如,如果我有三个控制器 - 海报、接收器 A 和接收器 B。在海报控制器的功能中,我执行以下操作:
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceiverADoSomething" object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ReceiverBDoSomething" object:self];
Run Code Online (Sandbox Code Playgroud)
在接收者 A 的 viewDidLoad 方法中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(workToDoByA:) name:@"ReceiverADoSomething" object:nil];
Run Code Online (Sandbox Code Playgroud)
在接收者 B 的 viewDidLoad 方法中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(workToDoByB:) name:@"ReceiverADoSomething" object:nil];
Run Code Online (Sandbox Code Playgroud)
会先做workToDoByA再做workToDoByB吗?还是会一起被处决?
另一种情况......如果我有海报 A 发布通知,但有两个观察者到同一通知。那么执行顺序是什么?
在此先感谢您的帮助。
我正在尝试将此代码从 Swift 3 更新到 Swift 4.2
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: .UIKeyboardDidShow, object: nil);
Run Code Online (Sandbox Code Playgroud)
到目前为止,我刚刚尝试了编译器给出的自动更正。这导致如下代码:
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: .UIResponder.keyboardDidShowNotification, object: nil);
Run Code Online (Sandbox Code Playgroud)
从 ios 9 开始,没有必要取消订阅通知中心,因为 ios 会自动处理这一点,但在 ios 9 之前,开发人员必须手动调用NotificationCenter.default.removeObserver(self)以避免内存泄漏,这是常见的地方(在很多教程中都建议和 Stackoverflow 帖子)deinit。所以,我的问题是 - 如何deinit从通知中取消注册,因为deinit只在对象释放之前调用,所以它的引用计数应该是 0,但肯定不是 - 因为我们仍然订阅通知中心。实现这一点的唯一可能方法似乎是使用弱引用,基本上如果通知中心弱引用对象,上述场景是可能的,但在这种情况下,根本不需要取消订阅,因为对象可以很容易地被释放。有人可以澄清一下这是如何工作的。
您能解释一下NSNotification的目的是什么,我可以使用哪些情况?
通知是否通过传递委托调用应用程序中的所有类,还是调用特定类?
是否可以创建1个通知,并在多个类中接收它?
我终于想出了如何使用apple Reachability文件,这是一件好事.
我的问题是,因为我有大约6个视图,每个视图都需要检查我是否连接到互联网.在我的应用程序中实现可访问性的最佳方法(包括NSNotificationCenter,以便在连接发生变化时自动知道)是什么,这样我就不会在每个类中复制和粘贴相同的代码.
我在我的app委托中假设一些东西.但是,如何实现NSNotificationCenter并让所有其他类知道连接何时发生变化?
我正在使用以下代码订阅我自己的通知:
NSNotificationCenter.DefaultCenter.AddObserver("BL_UIIdleTimerFired", delegate {
Console.WriteLine("BaseFolderViewController: idle timer fired");
});
Run Code Online (Sandbox Code Playgroud)
要发送通知:
NSNotificationCenter.DefaultCenter.PostNotificationName("BL_UIIdleTimerFired", null);
Run Code Online (Sandbox Code Playgroud)
但是,只有在"anObject"参数PostNotificationName(string sString, object anObject)不为NULL 时才会正确接收通知.
这是设计的吗?我必须传递一个物体吗?或者这是一个错误?我真的不想发送对特定对象的引用.
我需要在两个不同的控制台应用程序Observer和Client之间进行通信.
在Observer应用程序中,我添加了以下代码:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];
Run Code Online (Sandbox Code Playgroud)
在客户端应用中,我添加了以下代码:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];
-(void)trackNotification:(NSNotification*)notif
{
NSLog(@"trackNotification: %@", notif);
NSLog(@"trackNotification name: %@", [notif name]);
NSLog(@"trackNotification object: %@", [notif object]);
NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}
Run Code Online (Sandbox Code Playgroud)
但没有任何反应.我阅读了所有文档,但我在Mac OS X中是全新的.有什么想法吗?
我读到了分布式通知,我改变了我的代码,现在看起来像这样:在服务器端:
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];
-(void)trackNotification:(NSNotification*)notif
{
NSLog(@"trackNotification: %@", notif);
NSLog(@"trackNotification name: %@", [notif name]);
NSLog(@"trackNotification object: %@", [notif object]);
NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}
Run Code Online (Sandbox Code Playgroud)
在客户端:
NSMutableDictionary *info;
info = [NSMutableDictionary dictionary];
[info setObject:@"foo" forKey:@"bar"];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
object:nil
userInfo:info …Run Code Online (Sandbox Code Playgroud) 添加和删除观察者的最佳做法是NSNotificationCenter什么?
我想知道如果加入self作为观察员viewDidLoad和删除self的viewDidUnload就足够了.也许我应该删除self的dealloc为好.
也许需要考虑低内存条件.我可以看到添加viewDidLoad和删除dealloc有问题:viewDidUnload由于内存不足而被调用...然后viewDidLoad在再次显示视图时被调用...现在self已被添加为观察者两次没有删除(因为dealloc不是所谓的).
注意:我正在考虑一个self引用UIViewController子类的基本示例.
我正在上课来处理我所有的iBeacon测试.它的目的是开始寻找区域,范围信标,识别它们然后发送通知.代码如下.
我遇到的问题是应用程序运行速度非常慢,我知道iBeacons有延迟问题,有时只是停止工作(不会识别关闭信标).我知道,我的代码很混乱,在我清理它之前尝试对逻辑进行排序.我想知道我是否错过了一个逻辑缺陷(我的意思是,我想知道我引入了哪些逻辑漏洞!).
#import "dcBeaconManager.h"
@implementation dcBeaconManager
@synthesize currentBeaconState;
bool testRanging = false;
int firstRegionEntered = 0;
int beaconsRangedCount = 0;
- (void)initBeaconManager {
NSLog(@"initBeaconManager called");
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"digiConsRegion"];
[self.locationManager startMonitoringForRegion:self.beaconRegion];
[self.locationManager requestStateForRegion:self.beaconRegion];
currentBeaconState = @"initial";
}
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
NSLog(@"Started looking for regions");
[self.locationManager requestStateForRegion:self.beaconRegion];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"Region discovered");
if (firstRegionEntered == 0) {
NSLog(@"First time …Run Code Online (Sandbox Code Playgroud) 我在swift中玩NSNotificationCenter.这是我的代码main.swift
进口基金会
class T: NSObject {
func someAction(notification: NSNotification) {
println(notification.userInfo)
}
}
var dataDict = Dictionary<String, String>()
dataDict["test"] = "test"
dataDict["test1"] = "test1"
var t = T();
NSNotificationCenter.defaultCenter().addObserver(t, selector:"someAction", name: "someAction", object:nil)
NSNotificationCenter.defaultCenter().postNotificationName("someAction", object:nil, userInfo:dataDict)
let runloop = NSRunLoop.currentRunLoop();
runloop.run();
println("Done");
Run Code Online (Sandbox Code Playgroud)
在线postNotification,我有一个例外:

这是一些堆栈跟踪:
2014-07-24 08:58:59.959 testswiftcli2[2171:303] -[_TtC13testswiftcli21T someAction]: unrecognized selector sent to instance 0x1005005a0
2014-07-24 08:58:59.961 testswiftcli2[2171:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtC13testswiftcli21T someAction]: unrecognized selector sent to instance 0x1005005a0'
Run Code Online (Sandbox Code Playgroud)
当我试图检查实例时,我得到了这个:
(lldb) …Run Code Online (Sandbox Code Playgroud) ios ×6
swift ×3
iphone ×2
objective-c ×2
c# ×1
cocoa ×1
dealloc ×1
deinit ×1
ibeacon ×1
reachability ×1
swift3 ×1
swift4.2 ×1
viewdidload ×1
xamarin.ios ×1
xcode ×1
xcode6 ×1