Swift3: Live UiLabel update on user input

前端 未结 4 1396
面向向阳花
面向向阳花 2021-01-07 13:45

I\'ve searched everywhere and can\'t seem to find a clear answer on updating a label in real-time.

How do i go about updating a UILabel (real-time) with

相关标签:
4条回答
  • 2021-01-07 14:14

    Use the delegate method:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        label.text = textField.text
        return true
    }
    
    0 讨论(0)
  • 2021-01-07 14:21

    option 1: Directly you can make action of textfield with value changed

    @IBAction func textFieldValueChange(_ sender: UITextField) {
            self.label.text = sender.text
        }
    

    option 2: Give Delegate to TextField

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
         label.text = textField.text 
         return true
     }
    
    0 讨论(0)
  • 2021-01-07 14:33

    You are update your label in textField shouldChangeCharactersInRange Delegate method :

       func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
          // Update Your label here......
    
         YourLabel.text = textField.text 
         return true
     }
    
    0 讨论(0)
  • 2021-01-07 14:36

    step 1:Extend class to UITextFieldDelegate

    class ViewController: UIViewController,UITextFieldDelegate
    

    In ViewDidLoad()

    Step 2: Add Following code into ViewDidLoad()

    txtDemo.delegate = self
    
    txtDemo.addTarget(self, action: #selector(LabelChanged(_:)), for:.editingChanged)
    

    step 3: Make new Function as below:

    func LabelChanged(_ sender:Any) {
        lblDemo.text = txtDemo.text
    }
    

    Or Also use delegate method:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        lblDemo.text = txtDemo.text
        return true
    }
    

    Here lblDemo is UILabel outlet and txtDemo is UITextField outlet

    0 讨论(0)
提交回复
热议问题