メインビューコントローラーでtextFieldを作成し、データを別のVCに渡したいとします。ストーリーボードを使用せずに、すべてのUIをプログラムで作成しました。ストーリーボードや識別子を使用せずにデータを渡すswift 3
私は、次の操作を行って、ボタンに追加アクションの内部でデータを渡してみました:
let destination = InsuranceInformationViewController()
destination.customerNameLabel = customerNameTextField.text
しかし、これは私のために動作しません。どんな助けもありがとうございます。事前に感謝します。
UPDATE
`// CustomerInformationViewController
override func viewDidLoad() {
super.viewDidLoad()
view.addSubView(customernameTextField)
view.addSubView(continueButton)
view.backgroundColor = UIColor.white
}
let customerNameTextField: UITextField = {
let textField = UITextField()
textField.placeHolderText = "First, Last"
textField.borderStyle = UITextBorderStyle.RoundedRect
textField.translatesAutoresizingMaskIntoConstraints = false
return textField
}()
let continueButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor.blue
button.setTitle("Continue", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(.white, for: .normal)
button.addTarget(self, action:#selector(CustomerInformationViewController().continueOn), for: .touchUpInside)
return button
}()
@objc func continueOn(){
let destination = InsuranceInformationViewController()
destination.customerNameLabel = customerNameTextField.text
self.navigationController?.pushViewController(InsuranceViewController(), animated: true)
}
// InsuranceViewController
override func viewDidLoad() {
super.viewDidLoad()
view.addSubView(customerNameLabel)
view.backgroundColor = UIColor.white
}
let customerNameLabel: UILabel = {
let label = UILabel()
label.text = "Name"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
`
多くの質問に働く
希望:これは、あなたのアプリで作成している 'InsuranceInformationViewController'の唯一のインスタンスである
は、代わりにあなたはこのような何かをすべきか?あなたはそれを作成した後でそれを提示しますか?どのデータ型が 'customerNameLabel'で、どのように宣言されていますか? –
「動作しません」と定義します。 – rmaddy
ラベルプロパティにテキストを割り当てていますか? 'customerNameLabel'のセッターをオーバーライドし、そこでデバッグしてください。 – n00bProgrammer