[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];
    }
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值