This question already has an answer here:
- Passing Data between View Controllers 41 answers
I was wondering how I can pass an object from a view controller to another view controller in a tab application?
So far I have done this:
I created the 'Tab Application' in Xcode; I then get two view controllers (FirstViewController and SecondViewController). The FirstViewController has two textfields:
todoName
andtodoDetail
, and in the SecondViewController I would like to have a table view list of todos.Then I create a new class called
Todo
with two simple string properties (todoName
andtodoDetail
) and with a constructor to fill in those properties.The user would add input to the two text fields in FirstViewController, which then instantiates a Todo object, and then I put the todo object in an array.
What I want to do is to pass this array to the SecondViewController so I can fill up the table view. How can I do this?
This can be easily solved by using one of the three following solutions:
You should take some time and go through this tutorial: http://www.raywenderlich.com/46988/ios-design-patterns
So basically... 1. vc1 has vc2 as delegate and calls the methods define in the delegate protocol. 2. vc1 posts a notification and vc2 listens for it 3. vc1 calls a block on vc2 (very close to the delegate pattern)
I'm simply going to point out that your user experience is broken in this example. You'd be much better off having and 'add' button on the top right of your table view controller and presenting a new view controller modally to capture the ToDo values.