Search code examples
iosswiftiphonetextsend

how to send a text with a send button to text field on swift iOS


I really new. all I want to do is send in the text I got on the password field as a secure text to the text field if that's possible. kind of like sending a text but with the secure text. the secure button just lets you see what is inside. I got that to work but I don't know how to transfer the information from one place to the other. do I create a function? Im sorry im a complete noob.

import UIKit

class ViewController: UIViewController {

private let scrollView: UIScrollView = {
    let scrollView = UIScrollView()
    scrollView.clipsToBounds = true
    return scrollView
}()

private let secureButton : UIButton = {
    let button = UIButton()
    button.setTitle("Secure", for: .normal)
    button.backgroundColor = .link
    button.setTitleColor(.white, for: .normal)
    button.layer.cornerRadius = 12
    button.titleLabel?.font = .systemFont(ofSize: 20, weight: .bold)
    button.layer.masksToBounds = true
    button.addTarget( self, action: #selector(securebuttonTapped), for: .touchUpInside)
    return button
    
}()

let sendButton : UIButton = {
    let button = UIButton()
    button.setTitle("Send", for: .normal)
    button.backgroundColor = .green
    button.setTitleColor(.white, for: .normal)
    button.layer.cornerRadius = 12
    button.titleLabel?.font = .systemFont(ofSize: 20, weight: .bold)
    button.layer.masksToBounds = true
    button.addTarget( self, action: #selector(sendButtonTapped), for: .touchUpInside)
    return button
}()

let textView = UITextView()

private let passwordField: UITextField = {
    let field = UITextField()
    field.autocapitalizationType = .none
    field.autocorrectionType = .no
    field.returnKeyType = .done
    field.layer.cornerRadius = 12
    field.layer.borderWidth = 1
    field.layer.borderColor = UIColor.lightGray.cgColor
    field.placeholder = "Password"
    field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 0))
    field.leftViewMode = .always
    field.backgroundColor = .white
    field.isSecureTextEntry = true
    
    return field
}()




override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    view.backgroundColor = .gray
    
    
    
    
    view.addSubview(scrollView)
    scrollView.addSubview(passwordField)
    scrollView.addSubview(secureButton)
    scrollView.addSubview(textView)
    scrollView.addSubview(sendButton)

}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    
    scrollView.frame = view.bounds
    
    passwordField.frame = CGRect(x: 30,
                             y: 100,
                             width: scrollView.width-60,
                             height: 52)
    secureButton.frame = CGRect(x: 30,
                                y: passwordField.bottom+10 ,
                             width: scrollView.width-60,
                             height: 30)
    textView.frame = CGRect(x: 30,
                            y: secureButton.bottom+10 ,
                         width: scrollView.width-60,
                         height: 30)
    sendButton.frame = CGRect(x: 30,
                              y: textView.bottom+10 ,
                           width: scrollView.width-60,
                           height: 30)

}
    
@objc func securebuttonTapped() {
    let text = passwordField
    text.isSecureTextEntry = !text.isSecureTextEntry
}

@objc func sendButtonTapped() {
    let text = passwordField
    textView.inputView = text
    print("send button tapped")
}

}


Solution

  • Swift 5

    if you want to pass the data in the same controller its very simple. just asign the text to the textField on button Pressed

      var textField = UITexField()
        
      @objc func sendButtonTapped() {
        // here you will assign the text you got from the password field to watever textfield you want, whether it is secure text or simple text.
        
            textField.text = passwordField.text
            print("send button tapped")
        }
        
    

    if you want to pass that text to some other ViewController you need to send like this

      let vc = UIStoryboard(name: "Main", bundle: 
      nil).instantiateViewController(withIdentifier: "YourViewController") as! 
      YourViewController
    
      // here you will assign the text to the variable from other viewcontroller you 
      want to. 
    
      vc.text = "Enter the text here"
      self.navigationController?.pushViewController(vc, animated: true)