-1

I know that you can pass information between two view controllers if they are connected by a segue using

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        guard let destinationViewController = segue.destinationViewController as?  searchTermViewController else { return }
        destinationViewController.courseCodes = selectedCourses
    }
}

The above code gives an error if there is no segue because of the .destinationViewController. How do i pass information between to arbitrary view controllers without having to set up a global variable?

rohaldb
  • 589
  • 7
  • 24
  • 1
    Check http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Bretsko Jan 20 '16 at 22:52
  • if not segue then you should explain what you are using. If you are doing this by code than simply provide API in one of controllers. – Marek R Jan 20 '16 at 23:01

2 Answers2

0

Go to your storyboard, select the second view controller, go to the Identity inspector tab and give a StoryBoard ID value. This should be a unique value to identify your view controller.

Now in your first view controller', you can run this code. This will basically create an object of the second view controller, set the property value (for transferring data) and push it (same as the segue does)

let ctrl = self.storyboard?.instantiateViewControllerWithIdentifier("detailsView") 
                                                        as? SecondViewController
ctrl?.userId = 250 // data to pass.

self.navigationController?.pushViewController(ctrl!, animated: true)

provided userId is a variable in your SecondViewController class. Replace detailsView with the storyboard id value you gave earlier.

class SecondViewController: UIViewController {
     var userId : Int = 0   

    override func viewDidLoad() {
       super.viewDidLoad()
       // do something with self.userId
    }

}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Does the bottom code segment go in the first or second view controller? – rohaldb Jan 20 '16 at 23:04
  • That is the second view controller to which you want to navigate. It has a `usreId` property which we will set from the first controller ( code above) – Shyju Jan 20 '16 at 23:05
0

You can set up a delegate pattern in order to do this.

Here are the steps for setting up the delegate pattern between two objects, where object A is the delegate for object B, and object B will send messages back to A. The steps are:

  1. Define a delegate protocol for object B.
  2. Give object B an optional delegate variable. This variable should be weak.
  3. Make object B send messages to its delegate when something interesting happens, such as when it needs a piece of information. You write delegate?.methodName(self, . . .)
  4. Make object A conform to the delegate protocol. It should put the name of the protocol in its class line and implement the methods from the protocol.
  5. Tell object B that object A is now its delegate.

Here is a tutorial to give you a working example https://www.youtube.com/watch?v=9LHDsSWc680

MikeG
  • 3,745
  • 1
  • 29
  • 51