[IOS] Process pop-up box information through NSNotificationCenter

1. Register for notifications after clicking the event:

-(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. The controller where the popup box is located:

- (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. Receive feedback

#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 password. Optional.
    //If the encryption method is OPEN, you do not need to set a password
    //If the encryption method is SHARED, the password length must be 13 digits
    //If the encryption method is other, the password length must be 8-63 bits
    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];
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326295909&siteId=291194637