通知中心传值常用于界面之间的传值
在需要传值的界面发送通知,需要传的值写入字典,把字典放入userInfo:参数中
NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:userPhoneTextField.text,@"pwdCode", nil];
[[NSNotificationCenter defaultCenter]postNotificationName:@"loginResetPwd" object:nil userInfo:dict];
在需要接受的界面中,注册接受通知(这个界面必须在发送通知之前注册并存在,否则接不到通知),
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginResetPwd:) name:@"loginResetPwd" object:nil];
loginResetPwd:为接收到通知后处理的方法,因为通知中含有字典,所以接受时的参数为NSNotification类型,它含有三个属性:
@property (readonly, copy) NSString *name;
@property (nullable, readonly, retain) id object;
@property (nullable, readonly, copy) NSDictionary *userInfo;
其中userInfo就是推送通知时传值的字典!
-(void)loginResetPwd:(NSNotification *)dict
{
_pwdCode=[dict.userInfo objectForKey:@"pwdCode"];
NSLog(@"%@",dict.userInfo);
}