在两个视图控制器Swift之间传递变量时出错

时间:2017-03-23 01:17:39

标签: swift variables

我已经在这方面苦苦挣扎了一段时间,我已经接近拥有一个功能性的应用程序,所以拉我的头发。我从Facebook图形请求中获取了一些基本信息,并将该名称从字典中删除。然后我使用prepareforsegue将名称传递给第二个视图控制器。我计划在各种不同场景中使用“名称”作为唯一标识符。

不幸的是,我收到一个我不知道如何解决的错误:'Type of Type'UIStoryboardSegue'没有成员'SecondViewController'。我在第二个视图控制器中声明了var:var destViewController:String!

任何指针都会如此受到赞赏。我很沮丧,因为在这个工作之前我无法继续做任何事情。

代码在这里:

let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
        fbLoginManager.loginBehavior = FBSDKLoginBehavior.web
        fbLoginManager.logIn(withReadPermissions:     ["public_profile","email"], from: self) { (result, error) -> Void in
            if error != nil {
                print(error?.localizedDescription)
                self.dismiss(animated: true, completion: nil)
            } else if (result?.isCancelled)! {
                print("Cancelled")
                self.dismiss(animated: true, completion: nil)
            } else {

        }
    }

    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, relationship_status, gender, location, education"]).start(completionHandler: { (connection, result, error) -> Void in
        if (error == nil){
            let fbDetails = result as! NSDictionary
            print(fbDetails)

            if let userDataDict = result as? NSDictionary {
             let name = userDataDict["name"] as? String

                func prepareForSegue(segue: UIStoryboardSegue!, sender: Any!) {
                    if (segue.identifier == "loginToHome") {
                        //Checking identifier is crucial as there might be multiple
                        // segues attached to same view
                        var destViewController = segue.SecondViewController as? SecondViewController;
                        destViewController.name = name

                        var bookUniverse = [String]()

                    }


            }

        }
    }

1 个答案:

答案 0 :(得分:0)

您不应该在FBSDKGraphRequest中准备segue函数。你应该把它改为:

self.performSegueWithIdentifier("loginToHome", sender: self)

而不是仅传递名称,为什么不在第二个ViewController中有一个可以传递的模型对象?如果您已经设置了名称,请创建一个类变量并为其指定名称,然后将类变量传递给第二个viewcontroller。

func prepareForSegue(segue: UIStoryboardSegue!, sender: Any!) {
                if (segue.identifier == "loginToHome") {
                    //Checking identifier is crucial as there might be multiple
                    // segues attached to same view
                    var destViewController = segue.SecondViewController as? SecondViewController;
                    destViewController.name = name

                    var bookUniverse = [String]()

                }
}