这里有两个错误。我正在尝试获取按下按钮时出现的 UIAlertView。从那里,我希望用户输入一个新数字 (Double),然后让它更新变量 timeWorkedGoal 以及 timeWorkedGoalNumber 标签。
我收到错误“使用未解析的标识符‘UIKeyboardTypeNumberPad’”以及错误“闭包中对属性‘timeWorkedGoalNumber’的引用需要显式‘self’。使捕获语义显式。”有人知道我的语法是怎么回事吗?谢谢!
var timeWorkedGoal = 1.1
@IBOutlet weak var timeWorkedGoalNumber: UILabel!
@IBAction func resetTimeWorkedGoalButton(sender: AnyObject) {
var timeWorkedAlert = UIAlertController(title: "Time Worked", message: "Your current work time goal is \(timeWorkedGoal) hours – Enter your new goal.", preferredStyle: UIAlertControllerStyle.Alert)
timeWorkedAlert.addTextFieldWithConfigurationHandler {
(textField) in
}
timeWorkedAlert.addAction(UIAlertAction(title: "Submit", style: .Default, handler: {
(action) in
let textF = timeWorkedAlert.textFields![0] as UITextField
print(textF)
textF.keyboardType = UIKeyboardTypeNumberPad;
timeWorkedGoal = (Double("\(textF)"))!
print(timeWorkedGoal)
timeWorkedGoalNumber.text = "\(textF)"
}))
timeWorkedAlert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: {
(action) in
self.dismissViewControllerAnimated(true, completion: nil)
}))
self.presentViewController(timeWorkedAlert, animated: true, completion: nil)
}
最佳答案
Error "Use of unresolved identifier 'UIKeyboardTypeNumberPad'"
使用 textF.keyboardType = UIKeyboardType.NumberPad
Reference to property 'timeWorkedGoalNumber' in closure requires explicit 'self'
self 解释。而不是仅仅使用
timeWorkedGoalNumber
使用 self.timeWorkedGoalNumber
您还需要将 timeWorkedGoal = (Double("\(textF)"))!
更新为 self.timeWorkedGoal = (Double("\(textF)"))!
编辑:
textF
是一种 UITextField
,因此不能直接转换为 Double
。 Insted 您可以使用 textF.text
返回一个 String
。所以你会有这样的东西 Double("\(textF.text)")
关于Swift - UIAlertView 中的文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38035622/