Swift - UIAlertView 中的文本输入

标签 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)

}

最佳答案

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/

相关文章:

iphone - 如何从主视图中删除 UIAlertview

iphone - 如何在生成警报并按下 "OK"后清除文本字段

ios - 为什么 MapKit 框架必须链接到每个文件而 CoreData 框架不能?

ios - 如何将解析对象字段从一个类发送到另一个类?

ios - setBackButtonBackgroundImage 不工作

ios - iPhone不播放声音但模拟器可以播放声音

ios - 同时显示 UIAlertView 和键盘

ios - 警报的屏幕截图,iOS,Swift

ios - 当我在我的应用程序中按下一个按钮时,整个应用程序卡住

ios - 如何延迟提示位置服务权限