Pass objects to another view controller in a tab a

2019-03-02 15:05发布

This question already has an answer here:

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 and todoDetail, 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 and todoDetail) 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?

1条回答
甜甜的少女心
2楼-- · 2019-03-02 15:23

This can be easily solved by using one of the three following solutions:

  1. The delegate pattern
  2. The observer pattern
  3. Blocks or closures

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.

查看更多
登录 后发表回答