简体   繁体   English

如何在 Swift 中触摸 UILabel 后对其进行编辑?

[英]How can I edit a UILabel upon touching it in Swift?

I want to know how to edit the label of text while the app is running.我想知道如何在应用程序运行时编辑 label 文本。

Example: there will be label called "Tap to Change text."示例:将有 label 称为“点击更改文本”。 When the user clicks it, it will be editable and the user can input text and enter.当用户点击它时,它将变为可编辑状态,用户可以输入文本并进入。 Then it will change to new text.然后它将更改为新文本。

I know this can be done using UITextFieldDelegate, but I don't know how to approach to it because there is no way to put an action to a label when user touches it.我知道这可以使用UITextFieldDelegate,但我不知道如何处理它,因为当用户触摸它时无法对 label 执行操作。

You can not edit label like you edit textField but when user click on label you can hide label and unhide textField and when user finish entering text you can again hide textField and unhide label and you can assign textField's text to label this way:您不能像编辑 textField 那样编辑标签,但是当用户单击标签时,您可以隐藏标签并取消隐藏 textField,当用户完成输入文本时,您可以再次隐藏 textField 和取消隐藏标签,您可以通过这种方式将 textField 的文本分配给标签:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var lbl: UILabel!
    @IBOutlet weak var textF: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        textF.delegate = self
        textF.hidden = true
        lbl.userInteractionEnabled = true
        let aSelector : Selector = "lblTapped"
        let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)
        tapGesture.numberOfTapsRequired = 1
        lbl.addGestureRecognizer(tapGesture)
    }

    func lblTapped(){
        lbl.hidden = true
        textF.hidden = false
        textF.text = lbl.text
    }

    func textFieldShouldReturn(userText: UITextField) -> Bool {
        userText.resignFirstResponder()
        textF.hidden = true
        lbl.hidden = false
        lbl.text = textF.text
        return true
    }
}

Hope it will help.希望它会有所帮助。

To add my 2c here and remind me of the style in the latest Stanford University Paul Hegarty videos, the setup can be done in the "didSet" of the label Field - you can also set up different responders for different labels this way:在这里添加我的 2c 并让我想起最新的斯坦福大学 Paul Hegarty 视频中的风格,可以在标签字段的“didSet”中完成设置 - 您也可以通过这种方式为不同的标签设置不同的响应器:

@IBOutlet weak var lblField: UILabel! {
    didSet {
        let recognizer = UILongPressGestureRecognizer()
        recognizer.addTarget(self, action: #selector(ViewController.lbllongpress))
        lblField.addGestureRecognizer(recognizer)
    }
}

and then the implementation becomes:然后实现变成:

func lbllongpress(gesture: UILongPressGestureRecognizer) {
    switch gesture.state {
    case UIGestureRecognizerState.began:
        break;
    case UIGestureRecognizerState.ended:
        // Implementation here...
    default: break
    }
}

It would be better to make the label a UITextField instead.将标签改为 UITextField 会更好。 This gives the appearance of a label, and upon click it's editable.这给出了一个标签的外观,点击它是可编辑的。

  1. Create the UITextfield via storyboard通过故事板创建 UITextfield
  2. Connect the UITextfield outlet from the storyboard into the view controller file将故事板中的 UITextfield 插座连接到视图控制器文件中
  3. Assign the value of the outlet with an observer用观察者分配插座的值
     @IBOutlet weak var barTextField: UITextField! {
            didSet {
                self.barTextField.delegate = self // for step 4
                barTextField.text = "value here"
            }
        }
  1. Extend the viewcontroller with UITextFieldDelegate, which coincides with the delegate in step 3使用 UITextFieldDelegate 扩展 viewcontroller,与步骤 3 中的委托重合
  2. Implement several of the optional functions in UITextFieldDelegate, including textFieldShouldReturn to detect when the enter key was pressed在 UITextFieldDelegate 中实现几个可选函数,包括textFieldShouldReturn来检测何时按下回车键
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        barTextField.resignFirstResponder()
        return true
    }

Add TapGesture ( UITapGestureRecognizer ) to your label.将 TapGesture ( UITapGestureRecognizer ) 添加到您的标签。 Now when user tap on it you can enable the text field & in the text fields delegate method you can convert it to label.现在,当用户点击它时,您可以启用文本字段 & 在文本字段委托方法中,您可以将其转换为标签。

I made some adjustments on the selector from Dharmesh's answer.我根据 Dharmesh 的回答对选择器进行了一些调整。 Hope this helps.希望这可以帮助。

import UIKit进口 UIKit

class ViewController: UIViewController, UITextFieldDelegate { class 视图控制器:UIViewController,UITextFieldDelegate {

@IBOutlet weak var lbl: UILabel!
@IBOutlet weak var textF: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    textF.delegate = self
    textF.hidden = true
    lbl.userInteractionEnabled = true
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(lblTapped(_:))))
    tapGesture.numberOfTapsRequired = 1
    lbl.addGestureRecognizer(tapGesture)
}

@objc func lblTapped(_ sender:Any){
    lbl.hidden = true
    textF.hidden = false
    textF.text = lbl.text
}

func textFieldShouldReturn(userText: UITextField) -> Bool {
    userText.resignFirstResponder()
    textF.hidden = true
    lbl.hidden = false
    lbl.text = textF.text
    return true
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM