如何通过 2 个视图控制器传递 textfield.text 数据
How to pass textfield.text data through 2 view controllers
我试图在用户在 ViewController 中将两个文本字段(passName 和 passPhone)作为 textField.text 传递给我的第三个视图控制器,因为我需要它来引用 firebase数据库子名称,但每次我 运行 它都会给我一个 nil return 值?我不明白为什么值不通过
performSegue 应该将 textfield.text 传递给下一个视图控制器,因为在 override func prepare 中我将其设置为
错误代码:(在代码中查找位置)
线程 1:致命错误:在隐式展开可选值时意外发现 nil
// ViewController
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var numberField: UITextField!
@IBAction func linkTapped(_ sender: Any) {
if (nameField.text != nil && numberField.text != nil) {
if error != nil {
//code
} else {
self.performSegue(withIdentifier: "code", sender: self)
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = verificationCodeViewController()
vc.passName = self.nameField.text
vc.passPhone = self.numberField.text
nameField.endEditing(true)
numberField.endEditing(true)
}
//验证码ViewController
var passName: String!
var passPhone: String!
@IBAction func Login(_ sender: Any) {
if (code.text! != "") {
let defaults = UserDefaults.standard
let credential: PhoneAuthCredential = PhoneAuthProvider.provider().credential(withVerificationID: defaults.string(forKey: "authVID")!, verificationCode: code.text!)
Auth.auth().signIn(with: credential) { (user, error) in
if error != nil {
print("error: \(String(describing: error?.localizedDescription))")
} else {
print("Phone Number: \(String(describing: user?.user.phoneNumber))")
let userInfo = user?.user.providerData[0]
print("Provider ID: \(String(describing: userInfo?.providerID))")
self.performSegue(withIdentifier: "logged", sender: Any?.self)
}
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = RankedClubScreen()
vc.passName = self.passName
vc.passPhone = self.passPhone
}
// RankedClubScreen -- ViewController
var passName: String!
var passPhone: String!
@IBAction func signOutFunction(_ sender: Any) {
do {
try Auth.auth().signOut()
let ref = Database.database().reference().child("Online Users")
print(self.passName + self.passPhone) // Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
ref.child(self.passName + self.passPhone).removeValue()
self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)
} catch let error {
print("not signed in...", error)
}
}
您的问题是创建一个新的 vc 而不是在此处使用 segue 目的地
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = VerificationCodeViewController()
这里
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = RankedClubScreen()
就这样写吧
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! VerificationCodeViewController
vc.//// set this
让其他 segue 也这样做
我试图在用户在 ViewController 中将两个文本字段(passName 和 passPhone)作为 textField.text 传递给我的第三个视图控制器,因为我需要它来引用 firebase数据库子名称,但每次我 运行 它都会给我一个 nil return 值?我不明白为什么值不通过
performSegue 应该将 textfield.text 传递给下一个视图控制器,因为在 override func prepare 中我将其设置为
错误代码:(在代码中查找位置)
线程 1:致命错误:在隐式展开可选值时意外发现 nil
// ViewController
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var numberField: UITextField!
@IBAction func linkTapped(_ sender: Any) {
if (nameField.text != nil && numberField.text != nil) {
if error != nil {
//code
} else {
self.performSegue(withIdentifier: "code", sender: self)
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = verificationCodeViewController()
vc.passName = self.nameField.text
vc.passPhone = self.numberField.text
nameField.endEditing(true)
numberField.endEditing(true)
}
//验证码ViewController
var passName: String!
var passPhone: String!
@IBAction func Login(_ sender: Any) {
if (code.text! != "") {
let defaults = UserDefaults.standard
let credential: PhoneAuthCredential = PhoneAuthProvider.provider().credential(withVerificationID: defaults.string(forKey: "authVID")!, verificationCode: code.text!)
Auth.auth().signIn(with: credential) { (user, error) in
if error != nil {
print("error: \(String(describing: error?.localizedDescription))")
} else {
print("Phone Number: \(String(describing: user?.user.phoneNumber))")
let userInfo = user?.user.providerData[0]
print("Provider ID: \(String(describing: userInfo?.providerID))")
self.performSegue(withIdentifier: "logged", sender: Any?.self)
}
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = RankedClubScreen()
vc.passName = self.passName
vc.passPhone = self.passPhone
}
// RankedClubScreen -- ViewController
var passName: String!
var passPhone: String!
@IBAction func signOutFunction(_ sender: Any) {
do {
try Auth.auth().signOut()
let ref = Database.database().reference().child("Online Users")
print(self.passName + self.passPhone) // Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
ref.child(self.passName + self.passPhone).removeValue()
self.view.window!.rootViewController?.dismiss(animated: true, completion: nil)
} catch let error {
print("not signed in...", error)
}
}
您的问题是创建一个新的 vc 而不是在此处使用 segue 目的地
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = VerificationCodeViewController()
这里
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = RankedClubScreen()
就这样写吧
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! VerificationCodeViewController
vc.//// set this
让其他 segue 也这样做