我如何以编程方式创建 segue 以在视图控制器之间传递信息 - Swift
How can I create a segue programmatically to pass info between view controllers - Swift
我是 swift 的新人,我还没有找到关于这个的任何答案。我有一个游戏,其中有一个滑块和一个随机值,因此用户必须猜测滑块上的值。当用户猜测数字并按下 ok 按钮时,它会调用视图控制器。当他非常接近时,它会调用另一个视图控制器,最后如果他没有猜到,它会调用另一个视图控制器,然后用户重新启动。我的问题是,我如何以编程方式创建一个 segue,以便只需按 ok 即可将获得的积分和回合传递给每个视图控制器。这些视图控制器是在故事板中创建的,我通过代码调用它们
感谢您的帮助!
@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()
}
}
不,您不能以编程方式创建 segue。
关于信息传递,只需实例化下一个视图控制器并传递信息即可。推送或呈现编程。
NSString * storyboardName = @"MainStoryboard_iPhone";
NSString * viewControllerID = @"ViewID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
controller.infoDict = myDictToPass;
[self presentViewController:controller animated:YES completion:nil];
只需要为 swift 找到相同的代码...希望您能做到...
既然你说"Those view controllers are created in the storyboard"我相信你可以使用performSegueWithIdentifier
和prepareForSegue
:
在 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 标识符
当然你也必须在你的视图控制器中声明var point
:
class GonPerfectViewController: UIViewController {
var point = 0
override func viewWillAppear(animated: Bool) {
print(point)
}
}
您可以找到有关 segue 的更多信息 here
所以这应该等同于 Swift Nirav Gadhiya 的回答:
let storyboardName = "MainStoryboard_iPhone"
let viewControllerID = "ViewID"
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let controller = ViewController().storyboard?.instantiateViewControllerWithIdentifier(viewControllerID)
controller.infoDict = myDictToPass
self.presentViewController(controller!, animated: true, completion: nil)
希望对您有所帮助:)
并且 Swift 3,基于 Linus G 的答案进行开发,这是对 Nirav Gadhiya 的答案的重写:
let storyboardName = "Main"
let viewControllerID = "id_of_viewcontroller_specified_by_you_in_storyboard"
let storyboard = UIStoryboard(name: storyboardName, bundle:nil)
let controller = storyboard.instantiateViewController(withIdentifier: viewControllerID)
controller.infoDict = myDictToPass
//If it is a modal view:
//controller.modalPresentationStyle = .overFullScreen
self.present(controller, animated: false, completion: nil)
我是 swift 的新人,我还没有找到关于这个的任何答案。我有一个游戏,其中有一个滑块和一个随机值,因此用户必须猜测滑块上的值。当用户猜测数字并按下 ok 按钮时,它会调用视图控制器。当他非常接近时,它会调用另一个视图控制器,最后如果他没有猜到,它会调用另一个视图控制器,然后用户重新启动。我的问题是,我如何以编程方式创建一个 segue,以便只需按 ok 即可将获得的积分和回合传递给每个视图控制器。这些视图控制器是在故事板中创建的,我通过代码调用它们
感谢您的帮助!
@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()
}
}
不,您不能以编程方式创建 segue。
关于信息传递,只需实例化下一个视图控制器并传递信息即可。推送或呈现编程。
NSString * storyboardName = @"MainStoryboard_iPhone";
NSString * viewControllerID = @"ViewID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
controller.infoDict = myDictToPass;
[self presentViewController:controller animated:YES completion:nil];
只需要为 swift 找到相同的代码...希望您能做到...
既然你说"Those view controllers are created in the storyboard"我相信你可以使用performSegueWithIdentifier
和prepareForSegue
:
在 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 标识符
当然你也必须在你的视图控制器中声明var point
:
class GonPerfectViewController: UIViewController {
var point = 0
override func viewWillAppear(animated: Bool) {
print(point)
}
}
您可以找到有关 segue 的更多信息 here
所以这应该等同于 Swift Nirav Gadhiya 的回答:
let storyboardName = "MainStoryboard_iPhone"
let viewControllerID = "ViewID"
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let controller = ViewController().storyboard?.instantiateViewControllerWithIdentifier(viewControllerID)
controller.infoDict = myDictToPass
self.presentViewController(controller!, animated: true, completion: nil)
希望对您有所帮助:)
并且 Swift 3,基于 Linus G 的答案进行开发,这是对 Nirav Gadhiya 的答案的重写:
let storyboardName = "Main"
let viewControllerID = "id_of_viewcontroller_specified_by_you_in_storyboard"
let storyboard = UIStoryboard(name: storyboardName, bundle:nil)
let controller = storyboard.instantiateViewController(withIdentifier: viewControllerID)
controller.infoDict = myDictToPass
//If it is a modal view:
//controller.modalPresentationStyle = .overFullScreen
self.present(controller, animated: false, completion: nil)