lak*_*esh 3 nsnotification ios
我需要检查整个应用程序中你的wifi状态是否已经改变.我正在使用Reachability检查wifi状态是否打开.
我已经建立了一个像这样的观察者:
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
Run Code Online (Sandbox Code Playgroud)
问题是我需要将addObserver和removeObserver添加到所有viewcontrollers,并将reachabilityChanged函数添加到所有.
有没有更好的方法然后添加NSNotification我是否检查整个应用程序的无线状态?
需要一些指导和建议.谢谢.
Guo*_*uan 10
做一个super viewController叫rootViewController来subClass了UIViewController,并在init给init Notification,并在dealloc去除Notification
然后你viewController应该做subClass的一切rootViewController.它只是OOP
喜欢 :
@interface RootViewController : UIViewController
@end
@implementation RootViewController
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (id)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
}
return self;
}
- (void)reachabilityChanged:(NSNotification *)notification
{
// you can do nothing , it should be override.
}
Run Code Online (Sandbox Code Playgroud)
当你创建你的viewController时,你应该继承 RootViewController
@interface YourViewController : RootViewController
- (void)reachabilityChanged:(NSNotification *)notification
{
// if your super class method do some things you should call [super reachabilityChanged:notification]
// do your thing.
}
@end
Run Code Online (Sandbox Code Playgroud)
在implementation你应该实现的reachabilityChanged:方法
| 归档时间: |
|
| 查看次数: |
1057 次 |
| 最近记录: |