NSNotification 的麻烦

2023-11-29

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

-(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方法:

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

I have included the CoreWLAN framework to my project: enter image description here

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

请随时要求进一步详细说明。

更新(在达斯汀的回答之后):我创建了一个新项目,希望能够隔离问题。我按照您的描述设置了 .h 和 .m 文件。遗憾的是,我仍然没有收到任何通知。为了向您表明我没有撒谎(或疯狂),我提供了在同一运行时拍摄的两张(相当拥挤的)屏幕截图。注意: (1.我在handleNotification:方法中有一个断点。应用程序永远不会暂停。 (2.我添加了网络窗口来显示我的Machas在此运行期间确实改变了 Wi-Fi 网络。 (3.没有任何 NSLoged

Network 1: enter image description here

Network 2: enter image description here

2012 年 5 月 17 日更新:Dustin 的答案是正确的,但 Wi-Fi 接口名称会根据应用程序运行的硬件而有所不同。就我而言(MacBook Air;无以太网),我的 Wi-Fi 是 en0 而不是 en1。我设法从我妈妈的 iMac 上获取系统配置 plst 文件,Wi-Fi 名为 en1。以太网是en0。感谢大家的帮助。


为了获得这些通知,您需要持有 CWInterface 的实例。你的 .h 看起来像这样

#import <Cocoa/Cocoa.h>
@class CWInterface;

@interface AppDelegate : NSObject <NSApplicationDelegate>

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

@end

然后你的 .m 文件看起来像这样

#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 属性,这是重要的一点

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NSNotification 的麻烦 的相关文章

随机推荐