【发布时间】:2021-05-15 01:07:58
【问题描述】:
我真的很新。如果可能的话,我要做的就是将我在密码字段中获得的文本作为安全文本发送到文本字段。有点像发送文本但带有安全文本。安全按钮只是让您看到里面有什么。我让它工作,但我不知道如何将信息从一个地方转移到另一个地方。我要创建一个函数吗?对不起,我是个菜鸟。
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")
}
}
【问题讨论】:
-
发消息在哪里?到另一台设备?或其他一些视图控制器?如果其他一些设备你的意思是使用已经存在的应用程序,如 iMessage 或什么应用程序或纯文本?或者你想建立这种能力?你在制作一个完整的聊天应用程序吗?如果是,您需要将文本发送到您的服务器,您的服务器会将其发送给收件人
标签: ios swift iphone text send