iOS开发系列--通知与消息机制详解

2020-01-18 17:10:15于丽

主界面KCMainViewController.m:


//
// KCMainViewController.m
// NotificationCenter
//
// Created by Kenshin Cui on 14/03/27
// Copyright (c) 2014年 cmjstudio. All rights reserved.
//

#import "KCMainViewController.h"
#import "KCLoginViewController.h"
#define UPDATE_LGOGIN_INFO_NOTIFICATION @"updateLoginInfo"

@interface KCMainViewController (){
  UILabel *_lbLoginInfo;
  UIButton *_btnLogin;
}

@end

@implementation KCMainViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  [self setupUI];
}

-(void)setupUI{
  UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0, 100,320 ,30)];
  label.textAlignment=NSTextAlignmentCenter;
  label.textColor=[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1];
  _lbLoginInfo=label;
  [self.view addSubview:label];
  
  UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
  button.frame=CGRectMake(60, 200, 200, 25);
  [button setTitle:@"登录" forState:UIControlStateNormal];
  [button addTarget:self action:@selector(loginOut) forControlEvents:UIControlEventTouchUpInside];
  _btnLogin=button;
  
  [self.view addSubview:button];
}

-(void)loginOut{
  //添加监听
  [self addObserverToNotification];
  
  KCLoginViewController *loginController=[[KCLoginViewController alloc]init];
  
  [self presentViewController:loginController animated:YES completion:nil];
}

/**
 * 添加监听
 */
-(void)addObserverToNotification{
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLoginInfo:) name:UPDATE_LGOGIN_INFO_NOTIFICATION object:nil];
}

/**
 * 更新登录信息,注意在这里可以获得通知对象并且读取附加信息
 */
-(void)updateLoginInfo:(NSNotification *)notification{
  NSDictionary *userInfo=notification.userInfo;
  _lbLoginInfo.text=userInfo[@"loginInfo"];
  _btnLogin.titleLabel.text=@"注销";
}

-(void)dealloc{
  //移除监听
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
登录界面KCLoginViewController.m:

//
// KCLoginViewController.m
// NotificationCenter
//
// Created by Kenshin Cui on 14/03/27.
// Copyright (c) 2014年 cmjstudio. All rights reserved.
//

#import "KCLoginViewController.h"
#define UPDATE_LGOGIN_INFO_NOTIFICATION @"updateLoginInfo"

@interface KCLoginViewController (){
  UITextField *_txtUserName;
  UITextField *_txtPassword;
}

@end

@implementation KCLoginViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  [self setupUI];
}

/**
 * UI布局
 */
-(void)setupUI{
  //用户名
  UILabel *lbUserName=[[UILabel alloc]initWithFrame:CGRectMake(50, 150, 100, 30)];
  lbUserName.text=@"用户名:";
  [self.view addSubview:lbUserName];
  
  _txtUserName=[[UITextField alloc]initWithFrame:CGRectMake(120, 150, 150, 30)];
  _txtUserName.borderStyle=UITextBorderStyleRoundedRect;
  [self.view addSubview:_txtUserName];
  
  //密码
  UILabel *lbPassword=[[UILabel alloc]initWithFrame:CGRectMake(50, 200, 100, 30)];
  lbPassword.text=@"密码:";
  [self.view addSubview:lbPassword];
  
  _txtPassword=[[UITextField alloc]initWithFrame:CGRectMake(120, 200, 150, 30)];
  _txtPassword.secureTextEntry=YES;
  _txtPassword.borderStyle=UITextBorderStyleRoundedRect;
  [self.view addSubview:_txtPassword];
  
  //登录按钮
  UIButton *btnLogin=[UIButton buttonWithType:UIButtonTypeSystem];
  btnLogin.frame=CGRectMake(70, 270, 80, 30);
  [btnLogin setTitle:@"登录" forState:UIControlStateNormal];
  [self.view addSubview:btnLogin];
  [btnLogin addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
  
  //取消登录按钮
  UIButton *btnCancel=[UIButton buttonWithType:UIButtonTypeSystem];
  btnCancel.frame=CGRectMake(170, 270, 80, 30);
  [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
  [self.view addSubview:btnCancel];
  [btnCancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 登录操作
-(void)login{
  if ([_txtUserName.text isEqualToString:@"kenshincui"] && [_txtPassword.text isEqualToString:@"123"] ) {
    //发送通知
    [self postNotification];
    [self dismissViewControllerAnimated:YES completion:nil];
  }else{
    //登录失败弹出提示信息
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"系统信息" message:@"用户名或密码错误,请重新输入!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
    [alertView show];
  }
  
}

#pragma mark 点击取消
-(void)cancel{
  [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 * 添加通知,注意这里设置了附加信息
 */
-(void)postNotification{
  NSDictionary *userInfo=@{@"loginInfo":[NSString stringWithFormat:@"Hello,%@!",_txtUserName.text]};
  NSLog(@"%@",userInfo);
  NSNotification *notification=[NSNotification notificationWithName:UPDATE_LGOGIN_INFO_NOTIFICATION object:self userInfo:userInfo];
  [[NSNotificationCenter defaultCenter] postNotification:notification];
//也可直接采用下面的方法
//  [[NSNotificationCenter defaultCenter] postNotificationName:UPDATE_LGOGIN_INFO_NOTIFICATION object:self userInfo:userInfo];

}
@end