【发布时间】:2017-01-08 01:30:14
【问题描述】:
这是我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var maximumNumber: UITextField!
@IBAction func playButtonPressed(_ sender: Any) {
maximumNumber.isHidden = true
guessTextField.isHidden = false
}
@IBOutlet weak var guessTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
guessTextField.isHidden = true
maximumNumber.keyboardType = .numberPad
guessTextField.keyboardType = .numberPad
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
// Create an `NSCharacterSet` set which includes everything *but* the digits
let inverseSet = NSCharacterSet(charactersIn:"0123456789").inverted
// At every character in this "inverseSet" contained in the string,
// split the string up into components which exclude the characters
// in this inverse set
let components = string.components(separatedBy: inverseSet)
// Rejoin these components
let filtered = components.joined(separator: "") // use join("", components) if you are using Swift 1.2
// If the original string is equal to the filtered string, i.e. if no
// inverse characters were present to be eliminated, the input is valid
// and the statement returns true; else it returns false
return string == filtered
}
}
}
我希望文本字段 maximumNumber 和guessTextField 只允许整数。我尝试使用在这里找到的其他函数和委托,但它们无法工作或者我得到编译器错误。
【问题讨论】:
-
我尝试过使用我在这里找到的其他函数和委托,但它们无法工作或者我得到编译器错误但是在代码中 你 i> 显示,您正在做 nothing 来限制用户可以在此文本字段中输入的内容。什么都不会产生。显示一些代码。
-
由于它们不起作用,我已经删除了它们。
-
但在这种情况下,您只是要求某人从头开始为您编写整个代码。这不太合适。
-
我们不会跑去看看一些 pastebin。在此处显示您的代码作为您问题的一部分。
标签: ios swift swift3 uitextfield