如何通过 2 个视图控制器传递 textfield.text 数据
Posted
技术标签:
【中文标题】如何通过 2 个视图控制器传递 textfield.text 数据【英文标题】:How to pass textfield.text data through 2 view controllers 【发布时间】:2019-07-08 23:30:57 【问题描述】:当用户在 ViewController 中将两个文本字段(passName 和 passPhone)作为 textField.text 输入后,我尝试将它们传递给我的第三个视图控制器,因为我需要它来引用 firebase 数据库子名称,但每次我运行它给我一个零返回值?我不明白为什么值不通过
performSegue 应该将 textfield.text 传递给下一个视图控制器,因为在 override func prepare 中我设置了它
错误代码:(在代码中查找位置)
线程 1:致命错误:在隐式展开可选值时意外发现 nil
// 视图控制器
@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)
// 验证CodeViewController
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)
【问题讨论】:
【参考方案1】:你的问题是创建一个新的 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也这样做
【讨论】:
这不起作用,因为它在此代码行抛出“线程 1:信号 SIGABRT” let vc = segue.destination as!验证码视图控制器 将类名VerificationCodeViewController
分配给ib中的vc
对不起什么?您能否详细说明一下,我会投票并检查是否已回答
选择vc并将类设置为VerificationCodeViewController
查看编辑以上是关于如何通过 2 个视图控制器传递 textfield.text 数据的主要内容,如果未能解决你的问题,请参考以下文章
如何通过控制器将两个不同模型的值作为 Laravel 8 中的单个返回变量传递给视图文件