将对象传递到选项卡应用程序中的另一个视图控制器 [英] Pass objects to another view controller in a tab application
问题描述
到目前为止,我已经做到了这一点:
-
我在Xcode中创建了Tab应用程序然后我得到两个视图控制器(FirstViewController和SecondViewController)。 FirstViewController有两个文本字段:
todoName
和todoDetail
,在SecondViewController中,我想有一个表视图列表然后我用两个简单的字符串属性(<$ c)创建一个名为Todo
的新类: -
用户将向FirstViewController中的两个文本字段添加输入,然后实例化Todo对象,然后将todo对象放在数组中。 / p>
< $ c> todoName 和
todoDetail
)并使用构造函数填充这些属性。 我想做的是将此数组传递给SecondViewController,以便填满表视图。
这可以通过使用以下三个解决方案之一轻松解决:
- 代表模式
- 观察者模式
- li>
您应该花一些时间阅读本教程:
http://www.raywenderlich.com/46988/ios-design-patterns
所以基本上...
1. vc1具有vc2作为委托,并调用委托协议中定义的方法。
2. vc1发布通知,vc2监听它
3. vc1调用vc2上的块(非常接近代理模式)
我只是想指出,在这个例子中,你的用户体验被打破了。
你最好在表格视图控制器的右上角添加和添加按钮,并以模态方式呈现一个新的视图控制器来捕获ToDo值。
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:
- The delegate pattern
- The observer pattern
- 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.
这篇关于将对象传递到选项卡应用程序中的另一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!