[IOS]透过NSNotificationCenter处理弹出框信息
[IOS]通过NSNotificationCenter处理弹出框信息
1.点击事件后就注册通知:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; if (row > 2 && row < 7){ // [self ModifyWiFiInfoAlter]; NSString *okMsg = @"SureToModify"; NSString *cancleMsg = @"CancelToModify"; //OKClick cancelClick [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OKClick:) name:okMsg object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelClick:) name:cancleMsg object:nil]; UIStoryboard *board = [UIStoryboard storyboardWithName:@"WifiSetup" bundle:nil]; WiFISetupDetailController *WiFi_Info_Modify_VC = [board instantiateViewControllerWithIdentifier:@"WiFi_Info_Modify_VC"]; WiFi_Info_Modify_VC.infoDic = _infoDic; WiFi_Info_Modify_VC.showType = _showType; [self addChildViewController:WiFi_Info_Modify_VC]; [self.view addSubview:WiFi_Info_Modify_VC.view]; [WiFi_Info_Modify_VC didMoveToParentViewController:self]; } }
2.弹出框所在的controller:
- (IBAction)modifyWiFiInfoOKClick:(UIButton *)sender { NSLog(@"encrypt text:%@,channel text:%@",_encryption_text.text,_wifi_channel_text.text); _wifi_channel_text.text = [_infoDic objectForKey:@"WiFi Channel"]; _encryption_text.text = [_infoDic objectForKey:@"Encryption"]; NSLog(@"OK Button"); NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; NSString *okMsg = @"SureToModify"; [center postNotificationName:okMsg object:_infoDic]; } - (IBAction)modifyWiFiInfoCancelClick:(UIButton *)sender { NSLog(@"Cancel Button"); NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; NSString *cancleMsg = @"CancelToModify"; [center postNotificationName:cancleMsg object:nil]; }
3.接收反馈
#pragma mark - Modify WiFi info acion - (void) cancelClick:(NSNotification*)notification{ UIViewController *vc = [self.childViewControllers lastObject]; [vc.view removeFromSuperview]; [vc removeFromParentViewController]; [self removeModifyNotification]; } - (void) OKClick:(NSNotification*)notification{ NSMutableDictionary *dic = [notification object]; UIViewController *vc = [self.childViewControllers lastObject]; [vc.view removeFromSuperview]; [vc removeFromParentViewController]; [self removeModifyNotification]; // WIFI SSID NSString *ssid = [dic objectForKey:@"SSID"]; NSString *password = [dic objectForKey:@"Password"]; NSString *channel = [dic objectForKey:@"WiFi Channel"]; // NSString *powerLevel = [dic objectForKey:@"WiFi Strength"]; NSString *encrypt_str = [dic objectForKey:@"Encryption"]; NSLog(@"ssid:%@,pwd:%@,channel:%@,encrypt:%@",ssid,password,channel,encrypt_str); //WIFI 密码。可选。 //如果加密方式为OPEN,则无须设置密码 //如果加密方式为SHARED,密码长度必须为13位 //如果加密方式为其他,密码长度必须为8-63位 HwWifiEncryptMode encrypt = _toSetWifiInfo.encrypt; if (encrypt == kHwWifiEncryptModeOpen) { password = @""; }else if (encrypt == kHwWifiEncryptModeShared){ if(password.length == 0 || password.length !=13){ [MBProgressHUD showToast:self.view content:@"The password must contain 13 characters"]; return; } }else{ if (password.length == 0 || password.length < 8 || password.length > 63) { [MBProgressHUD showToast:self.view content:@"The password must contain 8 to 63 characters"]; return; } } NSLog(@"set wifi pwd:%@",password); _toSetWifiInfo.ssid = ssid; _toSetWifiInfo.password = password; // _toSetWifiInfo.powerLevel = powerLevel; _toSetWifiInfo.channel = channel; // _toSetWifiInfo.encrypt [self setWifiInfo:_toSetWifiInfo]; } -(void) removeModifyNotification{ NSString *okMsg = @"SureToModify"; NSString *cancleMsg = @"CancelToModify"; [[NSNotificationCenter defaultCenter]removeObserver:self name:okMsg object:nil]; [[NSNotificationCenter defaultCenter]removeObserver:self name:cancleMsg object:nil]; }