简体   繁体   English

iOS表格视图和视图控制器

[英]iOS Table Views and View Controller

I am trying to write my first iPad app, and I have a problem. 我正在尝试编写我的第一个iPad应用程序,但是我遇到了问题。

I have my Main view controller with a button. 我的主视图控制器带有一个按钮。 Once the button is pressed, it requests a list of information from a web server, and returns the data. 按下按钮后,它将向Web服务器请求信息列表,并返回数据。 This data, I want to put into a table view in a popover. 我想将这些数据放入弹出窗口的表视图中。

I have the main view controller, and the data is received back and put into a dictionary, then that is looped through to get a couple of arrays of data. 我有主视图控制器,然后将数据接收回来并放入字典中,然后循环遍历以获取几个数据数组。 I also have a Popover view controller that can display a static table view. 我还有一个Popover视图控制器,可以显示静态表格视图。

My question is, how do I get the data (either the dictionary, or the arrays) from the Main view controller to the Popover view controller? 我的问题是,如何从主视图控制器获取数据(字典或数组)到Popover视图控制器?

I am really lost. 我真的迷路了。

Thanks 谢谢

James 詹姆士

There are two design patterns you have to keep in mind: delegation and notification . 您必须牢记两种设计模式: 委托和通知 in a more general way also KVO is useful to update your view if the model changed. 如果模型发生更改,KVO还可以以更通用的方式更新视图。 Of course the object listening for notifications or acting as a delegate is the controller of that view, NEVER the view itself. 当然,侦听通知或充当委托的对象是该视图的控制器,而不是视图本身。

There are two things you want to avoid at any time: storing stuff in global variables or in singletons and making "super objects" that act as let us say the model and the controller. 您随时都需要避免两件事:将内容存储在全局变量或单例中,并制作“超级对象”以充当我们所说的模型和控制器。 Stick to the MVC paradigm and try to loose couple your objects and think about which object owns which other object. 坚持使用MVC范例,尝试松散耦合您的对象,并考虑哪个对象拥有另一个对象。 This is important for memory management and if you not implement it correctly it will come back to you later. 这对于内存管理很重要,如果您未正确实施它,则稍后会返回给您。

Check out session #116 - Model-View-Controller for iPhone OS from WWDC10 session videos and session #123 - iPhone View Controller Techniques from WWDC09. 从WWDC10会话视频中查看会话#116-iPhone OS的模型视图控制器,并从WWDC09中查看会话#123-iPhone视图控制器技术。

The WWDC10 videos are available for free at apple's developer site. WWDC10视频可在Apple的开发人员网站上免费获得。

Hey James, I suggest you first do your homework and try to get a taste on how so-called delegate structure works in most iOS apps. 嘿詹姆斯,我建议您先做作业,然后尝试一下大多数iOS应用程序中所谓的委托结构的工作方式。

To be more clear, your main view controller would become the delegate and your popover view controller would become the child of such a delegate. 更明确地说,您的主视图控制器将成为委托,而弹出窗口视图控制器将成为此类委托的子代 The delegate is responsible for presenting its child controller. 委托负责介绍其子控制器。 At the same time, the child controller is responsible for asking for data from and reporting any changes to its delegate controller. 同时,子控制器负责从其委托控制器请求数据并向其报告任何更改。

So in this way your popover controller can get the data it wants from your main view controller, and at the same time, when the user wants to dismiss the popover view, the popover view controller detects the user's instructions and informs the main view controller to dismiss it. 因此,以这种方式,您的弹窗控制器可以从主视图控制器获取所需的数据,同时,当用户想要关闭弹窗视图时,弹窗视图控制器会检测用户的指令并通知主视图控制器执行以下操作:消除它。

have described another possibility a few seconds ago: 几秒钟前描述了另一种可能性:

Passing array between view controllers? 在视图控制器之间传递数组?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM