Cesarg2199
Cesarg2199

Reputation: 579

How to access the same view from another view by multiple buttons

I am creating a game in swift and my goal is have each game level access the same view controller, but I need to send unique information from the button reaching that view controller so that I can access core data in the next view controller based on the information sent from the first view controller.

My problem is I do not know how to send multiple pieces of information from the first view controller to the second view controller in the prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)

I tried setting the unique information of each button in the keyPath storage, but I can't access it

LevelsMainScreen.swift override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { var dest : Levels = segue.destinationViewController as Levels //Getting the value of the keyPath of the tapped button to send to levels view controller dest.level = sender?.keyPath("levels") }

Here is the view controller I want to send it to so I can start to access core data information

class Levels: UIViewController {

    @IBOutlet var lblSubLevel: UILabel!
    @IBOutlet var lblLevel: UILabel!
    var level = Int()
    var subLevel = Int()

    override func viewDidLoad() {
         super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        lblLevel.text = "\(level.value)"

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

`

Upvotes: 0

Views: 208

Answers (1)

Suresh Kumar Durairaj
Suresh Kumar Durairaj

Reputation: 2126

You have to set an identifier for each segue from the storyboard in attributes inspector. Then, in the LevelsMainScreen.swift controller prepareForSeque method check for the segue identifier is equal to the repective segue identifier given by you. There you can assign the levels as you wanted.

ie., Name the the segue identifier as segue1, segue2, segue3 from attribute inspector in each segue.

In the LevelsMainScreen,

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

   var dest : Levels = segue.destinationViewController as Levels
   if(segue.identifier == "segue1") {
       //set your customized level values here...
       //ie., dest.level = 1
   }
   else if(segue.identifier == "segue2") {
       //set your customized level values here...
   }
   else if(segue.identifier == "segue3") {
       //set your customized level values here...
   }
}

Upvotes: 2

Related Questions