gpt4 book ai didi

ios - 如何在 swift 中使用 segue 发送用户信息以查看 Controller ?

转载 作者:行者123 更新时间:2023-11-29 02:13:31 26 4
gpt4 key购买 nike

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

这是 View Controller 。我想发送到 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.
}
*/

}

任何帮助将不胜感激。谢谢!

最佳答案

您应该在 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 是否是“startGame”segue,以防您想要其他不通过用户的 segue。

关于ios - 如何在 swift 中使用 segue 发送用户信息以查看 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29023800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com