微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用发送按钮将文本发送到 swift iOS 上的文本字段

如何解决如何使用发送按钮将文本发送到 swift iOS 上的文本字段

我真的很新。如果可能的话,我想要做的就是将我在密码字段中获得的文本作为安全文本发送到文本字段。有点像发送文本但使用安全文本。安全按钮只是让您看到里面的东西。我得到了它的工作,但我不知道如何将信息从一个地方传输到另一个地方。创建一个函数?对不起,我是个菜鸟。

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,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,height: 30)
    textView.frame = CGRect(x: 30,y: secureButton.bottom+10,height: 30)
    sendButton.frame = CGRect(x: 30,y: textView.bottom+10,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")
}

}

解决方法

Swift 5

如果你想在同一个控制器中传递数据,它非常简单。只需将文本分配给按钮按下的 textField

  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")
    }
    

如果你想将该文本传递给其他一些 ViewController,你需要像这样发送

  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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。