将文本值从一个视图控制器转移到另一个视图

时间:2017-04-20 12:04:11

标签: ios swift facebook facebook-graph-api swift3

所以我试图允许用户通过Facebook注册,这样他们就会点击Facebook按钮,授权我们的应用程序,然后将它们转到注册屏幕,其中包含字段" email"和"全名"填写我们从Facebook图形请求中收到的值。但是,我无法让它发挥作用。

这是Facebook按钮

的第一个视图控制器
import UIKit
import FBSDKLoginKit

  //[Facebook, 2017]
class RegisterVC: UIViewController, FBSDKLoginButtonDelegate {
var fbLoginSuccess = false

var fbName:String!
var fbEmail:String!

//[Facebook, 2017]
override func viewDidLoad() {
    super.viewDidLoad()

    let loginButton = FBSDKLoginButton()
    view.addSubview(loginButton)
    loginButton.frame = CGRect(x: 82, y: 325, width: view.frame.width - 210, height: 59)

    loginButton.delegate = self
}

//[Facebook, 2017]
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
    print("Did log out of facebook")
}

//[Facebook, 2017]
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if error != nil {
        print(error)
        return
    }
    print("Successfully logged in")

    FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {(connection, result, err) in
        if err != nil {
            print("Failed to start graph request", err)
            return
        } else {
            /*guard let data = result as? [String:Any] else {return}

            let fbEmail = data["email"] as! String
            let fbName  = data["name"] as! String

            print(fbEmail)
            print(fbName)

            func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                let vc = segue.destination as? CreateAccountVC
                vc!.emailTxtValue = self.fbName
                vc!.fullnameTxtValue = self.fbEmail
                */
            }

        guard let data = result as? [String:Any] else {return}

        let fbEmail = data["email"] as! String
        let fbName  = data["name"] as! String

        print(fbEmail)
        print(fbName)

        func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let vc = segue.destination as? CreateAccountVC {
            vc.emailTxtValue = self.fbName
            vc.fullnameTxtValue = self.fbEmail
            }
        }


        print(result)
    }

    performSegue(withIdentifier: "regSegue", sender: RegisterVC.self)

}

//override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    //get destination view an set the fullname



    //performSegue(withIdentifier: "regSegue", sender: RegisterVC.self)

//}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}





 }

这是下一个视图控制器,其文本字段应该显示从Facebook获取的信息

import UIKit

class CreateAccountVC: UIViewController {

var emailTxtValue: String?
var fullnameTxtValue: String?

@IBOutlet weak var usernameTxt: UITextField!
@IBOutlet weak var emailTxt: UITextField!
@IBOutlet weak var passwordTxt: UITextField!
@IBOutlet weak var fullnameTxt: UITextField!

override func viewDidLoad() {
    //fullname.text = fbName
    //email.text = fbEmail
    super.viewDidLoad()
    emailTxt.text = emailTxtValue
    fullnameTxt.text = fullnameTxtValue
    print(emailTxtValue)
    print("here")
    print(fullnameTxtValue)


}

func isValidEmail(emailTxt:String) -> Bool {
    // print("validate calendar: \(testStr)")
    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
    let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
    return emailTest.evaluate(with: emailTxt)
}

/*func isValidPassword(passwordTxt:String) -> Bool {
    let passwordRegEx = "{8,}"
    let passwordTest = NSPredicate(format:"SELF MATCHES %@", passwordRegEx)
    return passwordTest.evaluate(with: passwordTxt)
} */

func isValidUsername(usernameTxt:String) -> Bool {
    let usernameRegEx = "[a-z0-9_-]{5,16}"
    let usernameTest = NSPredicate(format:"SELF MATCHES %@", usernameRegEx)
    return usernameTest.evaluate(with: usernameTxt)
}

//[Idigov, 2017]
@IBAction func create(_ sender: AnyObject) {
    if usernameTxt.text!.isEmpty || passwordTxt.text!.isEmpty || emailTxt.text!.isEmpty || fullnameTxt.text!.isEmpty {

        usernameTxt.attributedPlaceholder = NSAttributedString(string: "username", attributes: [NSForegroundColorAttributeName: UIColor.red])
        emailTxt.attributedPlaceholder = NSAttributedString(string: "email", attributes:[NSForegroundColorAttributeName: UIColor.red])
        passwordTxt.attributedPlaceholder = NSAttributedString(string: "password", attributes: [NSForegroundColorAttributeName: UIColor.red])
        fullnameTxt.attributedPlaceholder = NSAttributedString(string: "fullname", attributes: [NSForegroundColorAttributeName: UIColor.red])

        let alertController = UIAlertController(title: "Error", message: "At least one of the values entered was not valid", preferredStyle: UIAlertControllerStyle.alert)
        alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler:nil))
        self.present(alertController, animated: true, completion: nil)
        // if there is text within any of the textboxes it will attempt to register

    } else if
        isValidEmail(emailTxt: emailTxt.text!) == false {
        let alertController = UIAlertController(title: "Error", message: "The email entered is not valid", preferredStyle: UIAlertControllerStyle.alert)
        alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler:nil))
        self.present(alertController, animated: true, completion: nil)

    } /*else if isValidPassword(passwordTxt: passwordTxt.text!) == false{
            let alertController = UIAlertController(title: "Error", message: "The password entered must be at least 8 characters long", preferredStyle: UIAlertControllerStyle.alert)
            alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler:nil))
            self.present(alertController, animated: true, completion: nil)

    } */else if isValidUsername(usernameTxt: usernameTxt.text!) == false {
        let alertController = UIAlertController(title: "Error", message: "Your username must be 5 to 16 characters long and can only contain letters, numbers, underscores or hyphens", preferredStyle: UIAlertControllerStyle.alert)
        alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler:nil))
        self.present(alertController, animated: true, completion: nil)
    }

    //[Idigov, 2017]
    else{


        let url = NSURL(string: "https://cgi.soic.indiana.edu/~team7/register.php")!

        let request = NSMutableURLRequest(url: url as URL)

        request.httpMethod = "POST"

        let body = "username=\(usernameTxt.text!.lowercased())&password=\(passwordTxt.text!)&email=\(emailTxt.text!)&fullname=\(fullnameTxt.text!)"
        request.httpBody = body.data(using: String.Encoding.utf8)

        URLSession.shared.dataTask(with: request as URLRequest as URLRequest, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) -> Void in

            if error == nil {
                DispatchQueue.main.async(execute: {

                    do {
                        let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
                        guard let parseJSON = json else {
                            print("Error while parsing")
                            return
                        }

                        let id = parseJSON["id"]

                        if id != nil && response != nil {
                            print(parseJSON)
                        }



                    } catch {
                        print("Caught an error \(error)")
                    }
                })

            } else {
                print("error: \(error)")
            }

        }).resume()
        let alertController = UIAlertController(title: "Registered", message: "Account Successfully Registered! Please Log In", preferredStyle: UIAlertControllerStyle.alert)
        let okay = UIAlertAction(title: "Okay", style: .default, handler: {_ in CATransaction.setCompletionBlock({
            self.performSegue(withIdentifier: "accountSuccess", sender: nil)})
            })
        alertController.addAction(okay)
        //alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler:nil))
        self.present(alertController, animated: true, completion: nil)
    }
    //performSegue(withIdentifier: "accountSuccess", sender: CreateAccountVC.self)
}



}

如果有人能帮我找到一个令人惊叹的解决方案!谢谢!

2 个答案:

答案 0 :(得分:3)

您需要将prepareForSegue方法放在类级别而不是任何其他方法中,您还需要在完成块内调用performSegue而不是在完成块之外。如下所示更改您的代码。

//[Facebook, 2017]
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if error != nil {
        print(error)
        return
    }
    print("Successfully logged in")

    FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start {(connection, result, err) in
        if err != nil {
            print("Failed to start graph request", err)
            return
        } else {
             guard let data = result as? [String:Any] else {return}
             self.performSegue(withIdentifier: "regSegue", sender: data)
        }
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? CreateAccountVC,
    let dic = sender as? [String:Any] {
        let fbEmail = dic["email"] as! String
        let fbName  = dic["name"] as! String
        vc.emailTxtValue = fbEmail
        vc.fullnameTxtValue = fbName
    }
}

答案 1 :(得分:0)

您已全局声明2个变量

var fbName:String!

var fbEmail:String!

当你为它分配值时,你正在创建相同的变量,

let fbEmail = data["email"] as! String

let fbName  = data["name"] as! String

所以你只需要从fbEmail和fbName中删除let,你就可以获得另一个View Controller的值