Swift - UIAlertView中的文本输入

时间:2016-06-26 05:51:56

标签: swift uialertview

我这里有两个错误。我正在尝试按下按钮时出现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)

}

2 个答案:

答案 0 :(得分:2)

  

错误“使用未解析的标识符'UIKeyboardTypeNumberPad'”

使用textF.keyboardType = UIKeyboardType.NumberPad

  

关闭时对属性'timeWorkedGoalNumber'的引用要求   明确的'自我'

解释自己。而不仅仅是使用  timeWorkedGoalNumber使用self.timeWorkedGoalNumber

您还需要将timeWorkedGoal = (Double("\(textF)"))!更新为self.timeWorkedGoal = (Double("\(textF)"))!

修改 textFUITextField的一种类型,因此无法直接转换为Double。您可以使用textF.textString。所以你会有这样的事情Double("\(textF.text)")

答案 1 :(得分:0)

第一个问题:

textF.keyboardType = UIKeyboardType.UIKeyboardTypeNumberPad

第二个问题:

添加

self.timeWorkedGoal = (Double("\(textF)"))!
而不是之前的

timeWorkedGoal = (Double("\(textF)"))!