Reputation: 143
I have a view controller that is called. It has some checks in it that if they are not met, it doesn't need to be show this view controller, and the next view controller in my application will be called. This is partially not working.
I have the following IBAction:
@IBAction func nextBtnPressed(sender: UIButton!) {
if GlobalVars.diceRollFirst == true {
performSegueWithIdentifier("showCombatOutcomeFromRearSupport", sender: self)
}
if GlobalVars.diceRollFirst == false {
performSegueWithIdentifier("showDiceRollFromRearSupport", sender: self)
}
}
Which works correctly.
However in my viewDidLoad() I have the following code: rearsupportfound is set earlier in the viewDidLoad after many checks. It's initially false and is set true if any of the checks are met.
if !rearsupportfound {
println("rearsupportfound is: \(rearsupportfound)")
if GlobalVars.diceRollFirst == true {
performSegueWithIdentifier("showCombatOutcomeFromRearSupport", sender: self)
}
if GlobalVars.diceRollFirst == false {
performSegueWithIdentifier("showDiceRollFromRearSupport", sender: self)
}
}
This code does nothing. By that I mean it does get to this code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "showCombatOutcomeFromRearSupport" {
println("segue: showCombatOutcomeFromRearSupport sender: \(sender)")
let vc = segue.destinationViewController as CombatOutcomeViewController
vc.army1 = army1
vc.army2 = army2
}
if segue.identifier == "showDiceRollFromRearSupport" {
println("segue: showDiceRollFromRearSupport")
let vc = segue.destinationViewController as DiceRollViewController
vc.army1 = army1
vc.army2 = army2
}
}
And I get the println entries, but the requested view controller is never presented and the current view controller is. The same perpareForSegue works perfectly when the button is pressed.
Any idea's why??
I realize that I should probably do the check for presenting this view controller before I even get here. I was trying to avoid writing the code twice, since the data displayed in the view hide's or display's certain labels and buttons. I haven't seen a good way for me to get around this, so since I had already written these checks, was trying to bypass the view controller within itself, depending on the values. Thoughts anyone? Thanks.
Upvotes: 1
Views: 399
Reputation: 66302
Your code isn't working because you're calling it from within viewDidLoad()
. You need to call it from viewDidAppear()
instead - see this question for more details.
As far as your code duplication issues, I would try to remove all game logic from your view controllers. Have a larger, higher level object that maintains the state of the game, and decides which view controllers are applicable at any given moment / scenario. Then this object can hand the appropriate segue identifier to the view controller. This way the view controller doesn't need to know about dice or combat or rear support.
Upvotes: 1