如何使用swift中的segue将用户信息发送到视图控制器?

时间:2015-03-13 02:15:06

标签: ios xcode swift parse-platform

我正在按照教程进行操作,我想稍微扩展一下这个功能。我目前有一个视图控制器,允许用户使用Parse查询浏览其他用户。当用户在用户图像上滑动时,我希望能够将所选择的用户信息发送到新控制器。我知道我必须使用prepareSegue,但我不确定如何将所选用户信息发送到该prepareSeque方法中的变量。

这是视图控制器。我想要发送给segue方法的用户被标记为"选择"以下是wasDragged方法:

import UIKit

class SwipeViewController: UIViewController {


    var xFromCenter: CGFloat = 0

    var usernames = [String]()
    var userImages = [NSData]()
    var currentUser = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        println(PFUser.currentUser()["first_name"])

        PFGeoPoint.geoPointForCurrentLocationInBackground { (geopoint: PFGeoPoint!, error: NSError!) -> Void in

            println(error)

            if error == nil {

                println(geopoint)

                var user = PFUser.currentUser()

                user["location"] = geopoint


                var query = PFUser.query()
                query.whereKey("location", nearGeoPoint:geopoint)

                query.limit = 10
                query.findObjectsInBackgroundWithBlock({ (users, error) -> Void in

                    var accepted = [String]()

                    if PFUser.currentUser()["accepted"] != nil {

                       accepted = PFUser.currentUser()["accepted"] as [String]

                    }

                    var rejected = [String]()

                    if PFUser.currentUser()["rejected"] != nil {

                       rejected = PFUser.currentUser()["rejected"] as [String]

                    }

                    for user in users {

                        var gender1 = user["gender"] as? NSString
                        var gender2 = PFUser.currentUser()["interestedIn"] as? NSString

                        if gender1 == gender2 && PFUser.currentUser().username != user.username && !contains(accepted, user.username) && !contains(rejected, user.username) {

                            self.usernames.append(user.username)

                            // Update - chaned as to as!

                            self.userImages.append(user["image"] as NSData)

                        }


                    }

                    var userImage: UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
                    println("images:")
                    println(userImage)
                    println(self.userImages)

                    userImage.image = UIImage(data: self.userImages[0])
                    userImage.contentMode = UIViewContentMode.ScaleAspectFit
                    self.view.addSubview(userImage)

                    var gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
                    userImage.addGestureRecognizer(gesture)

                    userImage.userInteractionEnabled = true



                })

                user.save()

            }

        }






    }

    func wasDragged(gesture: UIPanGestureRecognizer) {



        let translation = gesture.translationInView(self.view)
        var label = gesture.view!

        xFromCenter += translation.x

        var scale = min(100 / abs(xFromCenter), 1)

        label.center = CGPoint(x: label.center.x + translation.x, y: label.center.y + translation.y)

        gesture.setTranslation(CGPointZero, inView: self.view)

        var rotation:CGAffineTransform = CGAffineTransformMakeRotation(xFromCenter / 200)

        var stretch:CGAffineTransform = CGAffineTransformScale(rotation, scale, scale)

        label.transform = stretch

        if label.center.x < 100 {

            println("Not Chosen")

            PFUser.currentUser().addUniqueObject(self.usernames[self.currentUser], forKey: "rejected")
            PFUser.currentUser().save()

            self.currentUser++

        } else if label.center.x > self.view.bounds.width - 100 {

             //SEND THIS USER TO NEW CONTROLLER
            println("Chosen")

            PFUser.currentUser().addUniqueObject(self.usernames[self.currentUser], forKey: "accepted")
            PFUser.currentUser().save()

            //self.currentUser++

            performSegueWithIdentifier("startGame", sender: nil)

            return

        }

        if gesture.state == UIGestureRecognizerState.Ended {


            label.removeFromSuperview()

            if self.currentUser < self.userImages.count {

                var userImage: UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
                userImage.image = UIImage(data: self.userImages[self.currentUser])
                userImage.contentMode = UIViewContentMode.ScaleAspectFit
                self.view.addSubview(userImage)

                var gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:"))
                userImage.addGestureRecognizer(gesture)

                userImage.userInteractionEnabled = true

                xFromCenter = 0

            } else {

                println("No more users")

            }

        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        //var user = ?? I want to send this user info to the segue to GameViewController

        let destinationVC = segue.destinationViewController as GameViewController
    }


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


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */

}

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

你应该在GameViewController类中有一个用户变量,并在prepareForSegue函数中将它设置为刷过的用户。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "startGame") {
        let destinationVC = segue.destinationViewController as GameViewController
        destinationVC.user = self.currentUser; //or whatever variables you want to set
    }
}

我已经提供了代码来检查segue是否是&#34; startGame&#34; segue,以防你想要有其他不通过用户的segue。

答案 1 :(得分:0)

而不是在performSegueWithIdentifier:sender中为sender参数传递nil,而不是传递“Chosen”。您可以在该参数中传递所需的任何对象。然后它将是prepareForeSegue:sender:

中的sender参数