【问题标题】:Swift Reload textfield from button action从按钮操作中快速重新加载文本字段
【发布时间】:2014-11-13 21:35:14
【问题描述】:

我有一个按钮,可以创建更多带有文本字段的按钮:

func CreateButtonWithIndex(index:Int) {
  let newButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
  newButton.setTitle(“Button”, forState: UIControlState.Normal)
  newButton.addTarget(self, action: Selector("go:"), forControlEvents: UIControlEvents.TouchUpInside)

  newButton.tag = index+1;
  var textFieldButton: UITextField = UITextField()
  textFieldButton.tag = index+1;
  textFieldButton.text = “textfield”
  self.view.addSubview(textFieldButton)
  self.view.addSubview(newButton)
}

func go(sender: AnyObject) {
   println(“ok press button“)
}

如何检测与按下的按钮对应的文本字段?

【问题讨论】:

    标签: iphone swift ios8


    【解决方案1】:

    将发送者转换为 UIButton

    @IBAction func go(sender: AnyObject) {
       let yourButton = sender as UIButton
    
       println(yourButton.titleLabel.text)
    
    }
    

    更好的是,如果您确定只有按钮会调用“go”操作,您可以改为这样做:

    @IBAction func go(sender: UIButton) {
    
        println(sender.titleLabel.text)
    
        if(sender.titleLabel.text == "OK")
            println("OK button was pressed")
    
    }
    

    更多信息可以在 Apple's Strings & Characters Swift documentation 的“比较字符串”部分和我找到的@IBOutlet in this tutorial 中找到。

    【讨论】:

    • 我有错误 uibutton 没有名为“title”的成员
    • 是的...我修复了我的源示例。我最近混合了太多 MacOS 和 iOS 的东西。 “yourButton.titleLabel.text”就是这样做的方法。
    • 但是 UiLabel 没有名为“text”的成员,而是通过标签?
    • 一个UILabel 对象(即标题标签)有a property named ".text
    【解决方案2】:

    子类 UIButton。将 UITextField 变量添加到子类 UIButton。创建 UIButton 子类后分配新的 UITextField 变量。

    UIButton 子类:

    class TextFieldButton: UIButton {
        var textField: UITextField! // use this to reference the related text field
    }
    

    你其他类的代码:

        func CreateButtonWithIndex(index:Int) {
            let newButton = UIButton.buttonWithType(UIButtonType.System) as TextFieldButton // use our new subclass
            newButton.setTitle(“Button”, forState: UIControlState.Normal)
            newButton.addTarget(self, action: Selector("go:"), forControlEvents: UIControlEvents.TouchUpInside)
    
            newButton.tag = index+1;
            var textFieldButton: UITextField = UITextField()
            textFieldButton.tag = index+1;
            textFieldButton.text = "textfield"
    
            newButton.textField = textFieldButton  // set reference to the text field
            self.view.addSubview(textFieldButton)
            self.view.addSubview(newButton)
        }
    
        func go(sender: AnyObject) {
            println("ok press button")
            println(sender.textField) // now we can access the related text field
        }
    

    【讨论】:

    • 让 newButton = UIButton.buttonWithType(UIButtonType.System) as TextFieldButton // 使用我们的新子类没有错误
    • 你是说有错误吗?如果是这样,错误是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2018-08-13
    • 2021-09-26
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多