2017-11-27 13 views
-1

メインビューコントローラーで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 
}() 

`

+3

多くの質問に働く

let destination = storyboard?.instantiateViewController(withIdentifier: "YOUR_STORYBOARD_ID") as! InsuranceInformationViewController destination.customerNameLabel = customerNameTextField.text 

希望:これは、あなたのアプリで作成している 'InsuranceInformationViewController'の唯一のインスタンスである

は、代わりにあなたはこのような何かをすべきか?あなたはそれを作成した後でそれを提示しますか?どのデータ型が 'customerNameLabel'で、どのように宣言されていますか? –

+4

「動作しません」と定義します。 – rmaddy

+0

ラベルプロパティにテキストを割り当てていますか? 'customerNameLabel'のセッターをオーバーライドし、そこでデバッグしてください。 – n00bProgrammer

答えて

0

あなたが先を定義しますでは、InsuranceInformationViewControllerの新しいインスタンスを作成しています。

ただし、このインスタンスを参照しているときは、ビューに表示されているインスタンス化されていないビューコントローラではないため、変更しようとするものは表示されません。これは

+0

レスポンスありがとう、私はストーリーボードを使用していない場合、これは動作しますか? – Dwendel

+0

@Dwendelああ、あなたが問題だと思っています。あなたはloadView()をオーバーライドし、InsuranceInformationViewControllerに必要なすべてのメソッドを実装していると確信していますか? –

関連する問題