ios - Swift 3 将数据从一个 ViewController 传递到另一个 ViewController

标签 ios swift

我是 IOS 编程的新手,我真的不知道我在问什么,但我会尽力解释

我正在使用 Firebase 进行身份验证,然后我想获取 UID 并将其传递给不同的 VC 我遇到的问题是我无法将 var userID 获取到打印出 IBAction 的一侧,这是我到目前为止的位置,任何指针都会得到很好的欢呼,伙计们

@IBAction func creatAccountPressed(_ sender: Any) {
    if let email = emailTextField.text,
        let password = passwordTextField.text,
        let name = nameTextField.text {
        Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
            if let firebaseError = error {
                print(firebaseError.localizedDescription)
            }
            let userID            = user!.uid
            let userEmail: String = self.emailTextField.text!
            let fullName: String  = self.nameTextField.text!
            self.ref.child("users").child(userID).setValue(["Email": userEmail, "Name": fullName])
            self.userID1          = user!.uid as! String
        })
        print(self.userID1)
        presentLoggedInScreen()
    }
}

func presentLoggedInScreen() {
    let stroyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let loggedInVC: LoggedInVCViewController = stroyboard.instantiateViewController(withIdentifier: "loggedInVC") as! LoggedInVCViewController
    self.present(loggedInVC, animated: true, completion: nil)
}

最佳答案

将信息从一个 VC 传递到另一个 VC 的一种更简单的方法是通过启动器,或者通过您在呈现第二个 VC 之前设置的变量。

因为你是新手,现在尝试变量方法,所以如果说,你正在传递一个字符串:

class LoggedInVCViewController : UIViewController {

    var info : String? {
       didSet {
          if let newInfo = self.info {
             //do what ever you need to do here
          }
       }
    }

    override viewDidLoad() {
       super.viewDidLoad()

    }

}

func presentLoggedInScreen(yourInfo: String) { 
   let stroyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
   let loggedInVC:LoggedInVCViewController = 
   storyboard.instantiateViewController(withIdentifier: "loggedInVC") as! 
   LoggedInVCViewController 
   loggedInVC.info = yourInfo
   self.present(loggedInVC, animated: true, completion: nil) 
}

请注意,您始终可以为 info 使用任何其他变量类之王。您还可以对您的 VC 进行编程,使其在属性的 get 括号内执行一些方法,根据信息的内容填充一些字段,加载特定的 UI 等。

另一个有用的方法是走初始化路线,但不幸的是你不能将它与 Storyboard nibs 一起使用(遗憾的是我知道,但是这个 post 很好地解决了这个问题),但它仍然有用,只要你觉得足够舒服来初始化并以编程方式设计 VC(如果是你,我会尽快学习)。

您在 VC 的自定义初始化方法中传递一个变量:

class LoggedInVCViewController : UIViewController {

    var info : Any? {
        get {
            if let this = self.info {
                return this
            } else {
                return nil
            }
        } set {
            if let new = newValue {
                //
            }
        }
    }

    init(info: Any?) {
        //This first line is key, it also calls viewDidLoad() internally, so don't re-write viewDidLoad() here!!
        super.init(nibName: nil, bundle: nil)

        if let newInfo = info {
            //here we check info for whatever you pass to it
            self.info = newInfo
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

所以你会使用如下:

func presentLoggedInScreen(yourInfo: String) {
        let loggedInVC = LoggedInVCViewController(info: yourInfo)
        self.present(loggedInVC, animated: true, completion: nil)
}

显然,如前所述,此方法不能用于 Storyboard ,但非常有用,而且我相信您可以看到,它更加紧凑。

我建议您熟悉 Swift Properties的文档,以及一些博客提示,例如 this one .一段时间后,您会变得非常有创意。

要了解更多程序化方法,我强烈推荐这个 YouTube channel :Let's Build That App ,我个人还没有找到更好的编程方法 Swift 编程引用点。

不要犹豫,提出问题!

更新

您的 IBAction 应如下所示:

@IBAction func creatAccountPressed(_ sender: Any) {
    if let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text {
        Auth.auth().createUser(withEmail: email, password: password, completion: { user, error in
            if let firebaseError = error {
                print(firebaseError.localizedDescription)
                }


            let userID = user!.uid
            let userEmail: String = self.emailTextField.text!
            let fullName: String = self.nameTextField.text!

            self.ref.child("users").child(userID).setValue(["Email": userEmail, "Name": fullName])

           self.userID1 = user!.uid as! String

           print(self.userID1)

           presentLoggedInScreen(yourInfo: self.userID1)


        })

    }
}

关于ios - Swift 3 将数据从一个 ViewController 传递到另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44935196/

相关文章:

ios - 在单个 UITableView 中填充两种类型的 UITableViewCell

iphone - 无法在 UITableview 的单元格内单击 UIButton

ios - 如何让 react-native 在 Mac OS X El Capitan 中使用主机文件?

ios - 如何生成签名方法OAuth1

Swift "with"关键字

ios - 如何在ios swift中根据ID读取本地json文件?

iphone - 带有弯曲端的iOS Draw Rectangle

ios - UICollectionView Rotation 旋转后布局不正确

c# - 在 Regex C# 中允许撇号、点、符号、数字和字符

ios - 使用 CAShapelayer 的循环定时器