0

Hi this may be a simple question and I apologize if it is on here already, I searched for what I could but couldn't find an answer that tailors to my specific situation.

Here is the issue. I am currently creating a text adventure to practice some iOS development using swift in Xcode 6 and in this case I have a text view in my view controller that can scroll to the bottom of itself and 2 buttons at the bottom of the screen. The point being that if you hit one button it segues you to a different scenario and also alters some general character stats from a class UserCharacter I defined in my project altering some basic data and vice versa for the other button.

Thing is in my segue I need to check which button the user pressed so I know which data to segue but can't quite figure out how to do that.

Here is what I have so far, (I have already passed my data once previously but didn't need to make a choice so it was fairly simple)

class RouteZeroViewController: UIViewController {

var character : UserCharacter?

@IBAction func choiceOneButton(sender: AnyObject) {
    character?.strength += 3
    character?.intelligence -= 2
}
@IBAction func choiceTwoButton(sender: AnyObject) {
    character?.agility += 2
    character?.intelligence += 2
    character?.courage -= 2
}
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    // set initial textView load at the top instead of middle/bottom
    self.textView.scrollRangeToVisible(0)
}

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].
    // check for button one push ????????
    var thirdSceneA = segue.destinationViewController as RouteOneAViewController
    thirdSceneA.character = character

    // check for button two push ????????
    var thirdSceneB = segue.destinationViewController as RouteOneBViewController
    thirdSceneB.character = character
    }

}

Any help would be appreciated. Thank You..

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
tokyo0709
  • 1,897
  • 4
  • 28
  • 49

1 Answers1

0

You need to add segue from view controller to viewcontroller refer this link : IOS7, Segue and storyboards - How to create without a button?

set tag property to each button i.e. 1 and 2

on button click pass button

   @IBAction func choiceOneButton(sender: AnyObject) 
{
character?.strength += 3
character?.intelligence -= 2
 self.performSegueWithIdentifier("identifer", sender: sender)
}


 @IBAction func choiceTwoButton(sender: AnyObject)
{
character?.agility += 2
character?.intelligence += 2
character?.courage -= 2
self.performSegueWithIdentifier("identifer", sender: sender)
 }

In prepare segue method check

 override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) 
{
   var btn:UIButton=sender as UIButton

        if btn.tag==1
        {
            // First button clicked
        }

        if btn.tag==2
        {
            // Second button clicked
        }

}
Community
  • 1
  • 1
Kalpesh
  • 5,336
  • 26
  • 41
  • Ah ok. I think that worked out for me, I'm having a hard time seeing the variables as I'm in debug mode but it seemed to allow me to move on to the next scene. One question though, why do we have to have the line, self.performSegueWithIdentifier("identifier", sender: sender) if we already specified on the storyboard where each button will take us? – tokyo0709 Apr 01 '15 at 14:42