我是新手,我没有找到任何答案。我有一个游戏,其中有一个滑块和一个随机值,因此用户必须猜测滑块上的值。当用户猜出号码,然后按下确定按钮时,它会调用一个视图控制器。当他非常接近时,它会调用另一个视图控制器,最后如果他不猜它,它会调用另一个视图控制器,然后用户重新启动。我的问题是,如何通过编程方式创建一个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()
}
}
答案 0 :(得分:4)
因为你说&#34;那些视图控制器是在故事板中创建的#34;我相信您可以使用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
的信息答案 1 :(得分:1)
不,你不能以编程方式创建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找到相同的代码......我希望你能这样做......
答案 2 :(得分:1)
所以这应该与Swra of 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)
希望有所帮助:)
答案 3 :(得分:1)
使用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)