@interface KLMainViewController ()
@property (strong, nonatomic) UITextField *textField;
@property (strong, nonatomic) UIButton *button;
@end
@implementation KLMainViewController
- (void)dealloc {
//移除所有通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
// 移除某个
// [[NSNotificationCenter defaultCenter] removeObserver:self name:@"TransDataNoti" object:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"主界面";
//布局代码省略
......
//注册通知,用于接收通知,接收通知的名称必须和发送通知的名称保持一致才能接收到,否则无法接收到发出的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiReceived:) name:@"TransDataNoti" object:nil];
}
//接收通知,解析内容进行处理
- (void)notiReceived:(NSNotification *)sender {
self.textField.text = sender.userInfo[@"content"];
}
- (void) btnClicked:(UIButton *)btn {
KLSubViewController *subVC = [[KLSubViewController alloc] init];
[self.navigationController pushViewController:subVC animated:YES];
}
@end