首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSNotification故障

NSNotification故障
EN

Stack Overflow用户
提问于 2012-05-10 00:07:44
回答 1查看 1K关注 0票数 4

当我的类初始化时,它将自己添加为一堆不同Wi通知的观察者。由于某种原因,当这些事情发生时,选择器没有运行。有什么想法吗?提前谢谢你。

代码语言:javascript
复制
-(id) init
{
    if (self)
    {
        sself = self;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWModeDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWSSIDDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWBSSIDDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWCountryCodeDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWLinkDidChangeNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWPowerDidChangeNotification object:nil];

更新:下面是handleNotification方法:

代码语言:javascript
复制
-(void) handleNotification:(NSNotification*) notification
{
    NSLog(@"Notification Received");
}

我已经将CoreWLAN框架包含到我的项目中:

我已经下载了CoreWLANWirelessManager.app,这就是我要借鉴的内容。奇怪的是,苹果的代码使用的是不推荐的通知,而且它仍然有效。我尝试过使用新API和不推荐的API,但没有成功。我不确定我是否可以在这里发布他们的代码,但实际上没有区别。选择器甚至有相同的名称。

请不要犹豫,要求进一步详细说明。

更新(在达斯汀回答后):我创建了一个新项目,希望能隔离这个问题。正如您所描述的,我设置了我的.h和.m文件。遗憾的是,我还是没有收到任何通知。为了向您展示我没有说谎(或疯狂),我包含了两个(相当拥挤的)屏幕截图,这些截图都是在同一个运行时拍摄的。注意:(1. handleNotification:方法中有一个断点。这个应用程序从不停顿。(2 )我加入了网络窗口,以显示我的Mac在这个运行时确实改变了Wi网络。(3.没有什么是NSLoged

网络1:

网络2:

2012年5月17日更新:达斯汀的回答是正确的,但Wi接口的名称因应用程序运行的硬件而异。在我的例子中(MacBook Air;没有以太网),我的Wi是en0而不是en1。我设法从我妈妈的iMac上抓取了系统配置plst文件,而Wi被称为en1。以太网是en0。谢谢你们的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-12 13:46:23

为了获得那些通知,您需要保存CWInterface的一个实例。你的.h会像这样

代码语言:javascript
复制
#import <Cocoa/Cocoa.h>
@class CWInterface;

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (retain) CWInterface *wirelessInterface;

@end

那么,在您的.m文件中,应该如下所示

代码语言:javascript
复制
#import "AppDelegate.h"
#import <CoreWLAN/CoreWLAN.h>

@implementation AppDelegate

@synthesize window = _window;
@synthesize wirelessInterface;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWModeDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWSSIDDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWBSSIDDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWCountryCodeDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWLinkDidChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:CWPowerDidChangeNotification object:nil];

    self.wirelessInterface = [CWInterface interfaceWithName:@"en1"];
}


-(void) handleNotification:(NSNotification*) notification
{
    NSLog(@"Notification Received");
}

@end

注意CWInterface属性,这是重要的部分

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10525858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档