Ios 在不使用情节提要或标识符的情况下传递数据swift 3
我在主viewcontroller中创建了一个文本字段,希望将数据传递给另一个VC。我以编程方式创建了所有UI,没有脚本 我尝试通过执行以下操作在添加到按钮的操作中传递数据:Ios 在不使用情节提要或标识符的情况下传递数据swift 3,ios,iphone,swift,Ios,Iphone,Swift,我在主viewcontroller中创建了一个文本字段,希望将数据传递给另一个VC。我以编程方式创建了所有UI,没有脚本 我尝试通过执行以下操作在添加到按钮的操作中传递数据: let destination = InsuranceInformationViewController() destination.customerNameLabel = customerNameTextField.text 但这对我不起作用。感谢您的帮助,提前谢谢 更新 `//CustomerInformationV
let destination = InsuranceInformationViewController()
destination.customerNameLabel = customerNameTextField.text
但这对我不起作用。感谢您的帮助,提前谢谢
更新
`//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)
}
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
}()
//保险查看控制器
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)
}
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
}()
`当您定义destination
让destination=InsuranceInformationViewController()
时,您正在创建InsuranceInformationViewController的新实例
但是,在执行此操作时,当您引用该实例时,您尝试更改的任何内容都不会显示为有效,因为它不是您在视图中看到的已实例化的视图控制器
相反,您应该这样做:
let destination = storyboard?.instantiateViewController(withIdentifier: "YOUR_STORYBOARD_ID") as! InsuranceInformationViewController
destination.customerNameLabel = customerNameTextField.text
希望这能起作用很多问题:这是你在应用程序中创建的唯一一个
保险信息视图控制器的实例吗?您在创建后是否会显示它?什么数据类型是customerNameLabel
以及它是如何声明的?定义“不起作用”。是否将文本指定给标签属性?覆盖customerNameLabel
的设置程序,并在那里进行调试。@PhillipMills该操作会推送到InsuranceViewController。我喜欢:self.navigationController?.pushViewController(InsuranceViewController(),动画:true)这个:pushViewController(InsuranceViewController(),动画:true)
创建保险视图C的新副本遥控器
;它没有使用您分配标签时使用的标签。感谢您的回复,但如果我不使用故事板,这会起作用吗?@Dwendel哦,我知道您有问题,您确定您正在覆盖loadView()并在InsuranceInformationViewController上实现所有必要的方法吗?