gpt4 book ai didi

ios - 如何以编程方式创建 segue 以在 View Controller 之间传递信息 - Swift

转载 作者:可可西里 更新时间:2023-11-01 00:11:46 25 4
gpt4 key购买 nike

我是 swift 的新手,我还没有找到任何关于这个的答案。我有一个游戏,其中有一个 slider 和一个随机值,因此用户必须猜测 slider 上的值。当用户猜测数字并按下 ok 按钮时,它会调用 View Controller 。当他非常接近时,它会调用另一个 View Controller ,最后如果他没有猜到,它会调用另一个 View Controller ,然后用户重新启动。我的问题是,我如何以编程方式创建一个 segue,以便只需按 ok 即可将获得的积分和回合传递给每个 View Controller 。这些 View Controller 是在 Storyboard中创建的,我通过代码调用它们

感谢您的帮助!

@IBAction func okButton(sender: AnyObject) {

let diferencia = abs(targetValue - currentValue)
var point = 100 - diferencia
score += point

func bonusPoints() {
score += 100
}
if diferencia == 0 {

let viewController3 = self.storyboard!.instantiateViewControllerWithIdentifier("GonPerfectViewController") as UIViewController
presentViewController(viewController3, animated: true, completion: nil)
point += 100
newRound()
updateLabel()
bonusPoints()
} else if diferencia < 4 {

let viewController3 = self.storyboard!.instantiateViewControllerWithIdentifier("GonNiceTry1ViewController") as UIViewController
presentViewController(viewController3, animated: true, completion: nil)
point += 50
newRound()
updateLabel()

} else {

let viewController3 = self.storyboard!.instantiateViewControllerWithIdentifier("GonThirdViewController") as UIViewController
presentViewController(viewController3, animated: true, completion: nil)

startnewgame()
updateLabel()

}
}

最佳答案

既然你说“那些 View Controller 是在 Storyboard中创建的”,我相信你可以使用 performSegueWithIdentifierprepareForSegue:

okButton 方法之外声明您的变量。

    let targetValue = 10 // this is just to test the first case
let currentValue = 10 // this is just to test the first case
var score = 0
var point = 0

@IBAction func okButton(sender: AnyObject) {

let diferencia = abs(targetValue - currentValue)
point = 100 - diferencia
score += point
func bonusPoints(){
score += 100

}
if diferencia == 0{
performSegueWithIdentifier("GonPerfectViewController", sender: nil)
point += 100
print(point)
newRound()
updateLabel()
bonusPoints()
}else if diferencia < 4{

performSegueWithIdentifier("GonNiceTry1ViewController", sender: nil)

point += 50
newRound()
updateLabel()

}else{

performSegueWithIdentifier("GonThirdViewController", sender: nil)


startnewgame()
updateLabel()

}

}

然后你必须使用prepareForSegue:

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

if segue.identifier == "GonPerfectViewController"{

let vc = segue.destinationViewController as! GonPerfectViewController

vc.point = point
}
if segue.identifier == "GonNiceTry1ViewController"{

let vc = segue.destinationViewController as! GonNiceTry1ViewController

vc.point = point
}
if segue.identifier == "GonThirdViewController"{

let vc = segue.destinationViewController as! GonThirdViewController

vc.point = point
}


}

确保在每个 viewControllers 中设置 segue 标识符

enter image description here

当然,您还必须在 View Controller 中声明 var point:

class GonPerfectViewController: UIViewController {

var point = 0

override func viewWillAppear(animated: Bool) {
print(point)

}

}

您可以找到有关 segue 的更多信息 here

关于ios - 如何以编程方式创建 segue 以在 View Controller 之间传递信息 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34450745/

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