gpt4 book ai didi

objective-c - NSNotification 的麻烦

转载 作者:搜寻专家 更新时间:2023-10-30 19:44:42 24 4
gpt4 key购买 nike

当我的类初始化时,它会将自己添加为一堆不同 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");
}

我已将 CoreWLAN 框架包含到我的项目中: enter image description here

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

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

更新(在 Dustin 的回答之后):我创建了一个新项目,希望能找出问题所在。我按照您的描述设置了 .h 和 .m 文件。可悲的是,我仍然没有收到任何通知。为了向您展示我没有撒谎(或发疯),我提供了在同一运行时截取的两个(相当拥挤的)屏幕截图。注意:(1. 我在 handleNotification: 方法中有一个断点。应用程序永远不会暂停。(2. 我添加了网络窗口以显示我的 Mac 在此运行时确实更改了 Wi-Fi 网络。(3. 没有 NSLoged

网络 1: enter image description here

网络 2: enter image description here

2012 年 5 月 17 日更新:Dustin 的回答是正确的,但 Wi-Fi 接口(interface)名称因应用程序运行的硬件而异。在我的例子中,(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 属性,这是重要的一点

关于objective-c - NSNotification 的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10525858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com