Stuart Douglas
Stuart Douglas

Reputation: 137

Connecting instance of custom ViewController to a view

I'm working on the Card Matching Game project from Stanford's iOS course in Swift. I have two ViewControllers on a tab bar for 2 different types of card game that push to the same View, which shows move history. I want separate move histories for each game, so created a different instance of HistoryViewController in each of the 2 game-type's ViewController. I then manipulate these instances as moves are made. How do I tell Xcode to use that specific instance of HistoryViewController when navigating from the proper parent? For example, if I am in the 1st card game and press "History", the HistoryViewController initialized in card game 1's code should be shown in the History view.

I've attached an image of the storyboard. Any help would be much appreciated :)

Screenshot Of Storyboard

Upvotes: 0

Views: 121

Answers (1)

codester
codester

Reputation: 37189

You can push the history view controller from both view controllers.As they both will make the diffrent instance of history view controller.As both segue are different you can give different name to them and do self.performSegueWithIdentifier("push1", sender: self) and self.performSegueWithIdentifier("push2", sender: self) or directly you can push.

You can create the label or textview for history and pass the information which you want to display in

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "push1") {
        let viewController:ViewController = segue!.destinationViewController as ViewController
        // pass data to next view
        viewController.label = @"history for 1st view controller"
    }
    else if (segue.identifier == "someanothersegue") {
       //Perform some another task for any other segue
    }
}

This way you can pass data to destination view controller which is history view controller in your case.

Upvotes: 2

Related Questions