[IOS]如何设置弹出输入框,输入MAC并自动插入":"

-(void)showInputMacAlert{
    
    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:NSLocalizedStringFromTableInBundle(Input_MAC_Black_List_Alert_Title, nil, [[LanguageTool getInstance] getLocaleBundle], @"") message:NSLocalizedStringFromTableInBundle(Input_MAC_Black_List_Alert_Content, nil, [[LanguageTool getInstance] getLocaleBundle], @"") preferredStyle:UIAlertControllerStyleAlert];
    
    UIView *subView1 = alertControl.view.subviews[0];
    
    UIView *subView2 = subView1.subviews[0];
    
    UIView *subView3 = subView2.subviews[0];
    
    UIView *subView4 = subView3.subviews[0];
    
    UIView *subView5 = subView4.subviews[0];
    
    //IOS12 has a more uiview in subView5.subviews
    UILabel *titleView = subView5.subviews[0];
    UILabel *messageView = subView5.subviews[1];
    if ([titleView isMemberOfClass:[UIView class]]) {
        titleView = subView5.subviews[1];
        messageView = subView5.subviews[2];
    }
    
    titleView.textAlignment = NSTextAlignmentLeft;
    messageView.textAlignment = NSTextAlignmentLeft;
    
    //定义第一个输入框;
    [alertControl addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        [textField addTarget:self
                           action:@selector(textFieldDidEditing:)
                 forControlEvents:UIControlEventEditingChanged];
    }];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedStringFromTableInBundle(Confirm_Bt_Title, nil, [[LanguageTool getInstance] getLocaleBundle], @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
          //获取第1个输入框;
        UITextField *macTextField = alertControl.textFields.firstObject;
        BOOL isValidMac = [Util isValidMac:macTextField.text];
        if (isValidMac) {
            [self addDeviceToBlacklist:macTextField.text];
        }else{
            [MBProgressHUD showToast:self.view content:NSLocalizedStringFromTableInBundle(Mac_Invalid, nil, [[LanguageTool getInstance] getLocaleBundle], @"")];
        }

//        NSLog(@"controller text field content:%@", _macInputTextField.text);
        
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedStringFromTableInBundle(Cancel_Bt_Title, nil, [[LanguageTool getInstance] getLocaleBundle], @"") style:UIAlertActionStyleCancel  handler:^(UIAlertAction *action) {
    }];
    
    
    
    [alertControl addAction:cancelAction];
    [alertControl addAction:okAction];
    
    [self presentViewController:alertControl animated:YES completion:nil];
    
}

#pragma mark - textfield delegate
-(void)textFieldDidEditing:(UITextField*)textField{
    
    NSMutableString *inputText = [textField.text mutableCopy];
    NSLog(@"input text-1:%@",inputText);
    //Remove all ':' to caculate length
    NSString *tempString = [textField.text stringByReplacingOccurrencesOfString:@":" withString:@""];
    if (tempString.length !=0 && tempString.length%2==0 && textField.text.length<17 && ![_inputMacContentLastString isEqualToString:@":"]) {
        [inputText appendString:@":"];
        textField.text = inputText;
    }
    
    if (inputText.length > 0) {
         _inputMacContentLastString = [inputText substringFromIndex:inputText.length-1];
    }
    
}

相关推荐