在文本字段中为现有号码添加号码

时间:2018-04-06 00:50:31

标签: swift

如何指定按钮以获取文本字段中的当前数字并添加加1。然后,文本字段中的数字将生成条形码。

 @IBAction func generatePressed(_ sender: UIButton) {

    if dataField.text?.isEmpty ?? true {
        let alertController = UIAlertController(title: "Get me outta here!", message:
            "Enter a ticket #", preferredStyle: UIAlertControllerStyle.alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))

        self.present(alertController, animated: true, completion: nil)
        print("textField is empty")
    } else {
        if let text = dataField.text {

            let data = text.data(using: .ascii, allowLossyConversion: false)

            filter = CIFilter(name: "CICode128BarcodeGenerator")
            filter.setValue(data, forKey: "inputMessage")
            let transform = CGAffineTransform(scaleX: 10, y: 10)
            let image = UIImage(ciImage: filter.outputImage!.transformed(by: transform))

            displayCodeView.image = image
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我认为你应该检查你的textField的文字是不是一个数字而不只是检查isEmpty

@IBAction func generatePressed(_ sender: UIButton) {
    guard let number = Int(dataField.text ?? "") else {
        let alertController = UIAlertController(title: "Get me outta here!", message:
        "Enter a ticket #", preferredStyle: UIAlertControllerStyle.alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))
        self.present(alertController, animated: true, completion: nil)
        print("textField is empty")
        return
    }

    let data = "\(number + 1)".data(using: .ascii, allowLossyConversion: false)

    filter = CIFilter(name: "CICode128BarcodeGenerator")
    filter.setValue(data, forKey: "inputMessage")
    let transform = CGAffineTransform(scaleX: 10, y: 10)
    let image = UIImage(ciImage: filter.outputImage!.transformed(by: transform))

    displayCodeView.image = image
}

答案 1 :(得分:-2)

您可以轻松实现这一目标:

inoremap > ><c-o>:silent! iunmap <c-v><tab<c-v>><cr>              
inoremap < <<c-o>:silent! imap <c-v><tab<c-v>> <Plug>SuperTabForward<cr>

这假设您有一个默认值。如果不这样做,请设置默认值。

您可以使用@IBAction func generatePressed(_ sender: UIButton) { dataTextField.text = String(Int(dataTextField.text!)! + 1) } 作为小数值:

Double