我有一个简单的注册故事板,询问用户他们的姓名等。我正在收集输入的数据作为UITextfields,并希望将它们全部传递给最终的viewcontroller,在那里我将它全部变成一个阵列并将其发送到服务器。 JSON但是在添加测试数据的情况下工作正常。
BaseViewController
struct NewUserStruct {
var firstName: String
var lastName: String
}
发送VC
let firstNameReg = inputFirstName.text
let lastNameReg = inputLastName.text
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVC = segue.destination as? RegistrationUpdateViewController {
let newuser = NewUserStruct(firstName: firstNameReg! , lastName: lastNameReg! )
print(newuser)
destVC.newuser = newuser
} }
和接收VC
var newuser: NewUserStruct?
func createUser(){
guard let user = self.newuser else {
print("User is not set")
return
}
let service = "registration"
let parameters: [String: Any] = ["firstName": user.firstName,
"lastName": user.lastName,
"email": "Johnsmith@test11.com",
"password": "12345678",
"region": "EU",
"bundleId": Bundle.main.bundleIdentifier!,
"appVersion": Bundle.main.infoDictionary?["CFBundleVersion"] as! String]
这个编译但是我得到了一个打印值(newuser)的值为nil,它打印出警卫“用户未设置”
这是我的整个VC
import Foundation
import UIKit
class RegistrationNameViewController: BaseViewController {
@IBOutlet var inputFirstName: UITextField!
@IBOutlet var inputLastName: UITextField!
override func setup() {
}
override func navbarTitle() -> String {
return "Name"
}
override func leftNavbarButtonType() -> NavbarButtonType {
return NavbarButtonType.eNavbarButtonTypeBack
}
override func rightNavbarButtonType() -> NavbarButtonType {
return NavbarButtonType.eNavbarButtonTypeNext
}
override func navbarViewNextPressed(_ navbarView: NavbarView) {
let firstNameReg = inputFirstName.text
let lastNameReg = inputLastName.text
print(inputFirstName.text!,inputLastName.text!)
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVC = segue.destination as? RegistrationUpdateViewController {
print(destVC)
let newuser = NewUserStruct(firstName: firstNameReg! , lastName: lastNameReg! )
print(newuser)
destVC.newuser = newuser
} }
self.performSegue(withIdentifier: "NameToEmail", sender: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
我会使用一个结构,以便您正确定义要传递的数据。
struct NewUser {
var firstName: String
var lastName: String
}
现在您可以创建此结构的实例并将其传递到prepare(for segue:)
override func navbarViewNextPressed(_ navbarView: NavbarView) {
guard let firstName = inputFirstName.text where !firstName.isEmpty
let lastName = inputLastName.text where !lastName.isEmpty else {
print("first name and last name are required")
return
}
let newUser = NewUser(firstName: firstName, lastName: lastName)
self.performSegue(withIdentifier: "NameToEmail", sender: newUser)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVC = segue.destination as? RegistrationUpdateViewController {
print(destVC)
if let newuser = sender as? NewUser {
print(newuser)
destVC.newuser = newuser
}
}
}
在目标视图控制器中:
class RegistrationUpdateViewController {
var user: NewUser?
func someFunction {
guard let user = self.user else {
print("User is not set")
return
}
let service = "registration"
let parameters: [String: Any] = ["firstName": user.firstName,
"lastName": user.lastName,
"email": "Johnsmith@test11.com",
"password": "12345678",
"region": "EU",
"bundleId": Bundle.main.bundleIdentifier!,
"appVersion": Bundle.main.infoDictionary?["CFBundleVersion"] as! String]
...
}
}