简体   繁体   English

寻求有关iPhone应用程序结构的建议

[英]Seeking advice for iPhone app structure

I've been tasked with creating an iPhone app which will tap into a series of REST API JSON feeds from Twitter. 我的任务是创建一个iPhone应用程序,该应用程序将利用Twitter的一系列REST API JSON提要。

I'm somewhat new to iPhone development, but I've created a few apps before, though none of them required tapping into API data. 我对iPhone开发有些陌生,但是我之前已经创建了一些应用程序,尽管它们都不需要利用API数据。

So let's say we have 3 views in Interface Builder that are being controlled by a tabcontroller: 假设我们在Interface Builder中有3个视图,这些视图由tabcontroller控制:

  • Feed 饲料
  • Followers 追随者
  • Following 以下

Each of these views need to display a JSON feed in a tableview. 这些视图中的每一个都需要在表视图中显示JSON feed。 The data should be loaded at the time of the view being shown. 数据应在显示视图时加载。

There will also be a searchbox that makes a request to Twitter's search API JSON feed and returns results in a UITableView in another view. 还有一个搜索框,它向Twitter的搜索API JSON提要发出请求,并在另一个视图的UITableView中返回结果。

I've installed and imported the json-interface library in my project. 我已经在项目中安装并导入了json-interface库。 I have also successfully received the feed in an NSString. 我也已经成功接收了NSString中的提要。

But here's my questions: 但是,这是我的问题:

  • How should I prep my application to make requests to multiple JSON feeds and display the respective data in different tables on different views? 我应该如何准备我的应用程序以请求多个JSON feed,并在不同视图的不同表中显示各自的数据? All the tutorials I see only show one JSON request being formatted into one table on one view. 我所看到的所有教程仅显示了一个JSON请求在一个视图上被格式化为一张表。 For instance: http://iosdevelopertips.com/cocoa/json-framework-for-iphone-part-2.html 例如: http : //iosdevelopertips.com/cocoa/json-framework-for-iphone-part-2.html

  • How does Interface Builder fit into this? Interface Builder如何适应这一需求? How can I use the tables I inserted into my views in Interface Builder? 如何使用在Interface Builder中插入到视图中的表?

What I'm suggesting below may or may not be a bit more than what your situation requires, but it's always a good idea to separate the data and networking from you user interface classes. 我在下面的建议可能会或可能不会超出您的情况所需,但将数据和网络与用户界面类分开始终是一个好主意。

You should create a model layer, in other words a set of classes that are only responsible for getting data from the server and making it accessible to the rest of the app. 您应该创建一个模型层,换句话说,就是一组类,它们仅负责从服务器获取数据并使应用程序的其余部分可以访问该数据。 The way to design a model layer is similar to designing a database: think about the data domain and what the most convenient way of mapping it is. 设计模型层的方法类似于设计数据库:考虑数据域以及最方便的映射方法。 (In this case your model classes will follow the twitter data model rather closely.) It is also important not to think too much about what the interface will look like. (在这种情况下,您的模型类将非常接近Twitter数据模型。)同样重要的是,不要过多考虑界面的外观。 The model layer should be as independent of the UI as possible, so that changing the UI will not require changes to the model classes. 模型层应尽可能独立于UI,以便更改UI将不需要更改模型类。

Create a base model object that you can then subclass to represent specific kinds of data. 创建一个基础模型对象,然后可以将其子类化以表示特定种类的数据。 This base model should know how to make a call to the server, and it should have a state showing if the data is loading, completed, or if the loading has been canceled. 该基本模型应该知道如何调用服务器,并且应该具有一个状态,该状态显示数据是正在加载,已完成还是已取消加载。 UI classes such as views and controllers can observe this state and update themselves when it changes. UI类(例如视图和控制器)可以观察此状态并在状态更改时自行更新。

For example, each table view cell might have a reference to a model object, and when the model's state changes to "loaded", the cell will get some strings and other data from the model and update its appearance. 例如,每个表视图单元格可能都有对模型对象的引用,并且当模型状态更改为“已加载”时,该单元格将从模型中获取一些字符串和其他数据并更新其外观。

Take care not to have too many network requests going at the same time. 注意不要同时有太多网络请求。 Instead, use a queue. 而是使用队列。 You should definitely use the fantastic ASIHTTPRequest library for networking and queuing. 您绝对应该使用出色的ASIHTTPRequest库进行联网和排队。 To work with a model layer as outlined, you also need to understand Key-Value Observing . 要使用概述的模型层,还需要了解键值观察

As for your second question, it's very general. 至于第二个问题,这很笼统。 You use the tables by making your controller the dataSource and delegate and implementing the UITableViewDelegate and UITableViewDataSource protocols. 通过使控制器成为dataSourcedelegate并实现UITableViewDelegateUITableViewDataSource协议,可以使用这些表。

How should I prep my application to make requests to multiple JSON feeds and display the respective data in different tables on different views? 我应该如何准备我的应用程序以请求多个JSON feed,并在不同视图的不同表中显示各自的数据? ... ...

Each ViewController will take care of their own JSON requests like the tutorial. 每个ViewController都会像教程一样照顾自己的JSON请求。

How does Interface Builder fit into this? Interface Builder如何适应这一需求? How can I use the tables I inserted into my views in Interface Builder? 如何使用在Interface Builder中插入到视图中的表?

You create a IBOutlet for your tableviews in your ViewControllers. 您可以在ViewControllers中为表视图创建IBOutlet。 For example: 例如:

// FeedViewController.h
@interface FeedViewController : UIViewControllerUIViewController <UITableViewDelegate, UITableViewDataSource, FeedControllerDelegate> {
IBOutlet UITableView _feedTableView
}

In Interface Builder you link your TableView to the IBOutlet variable. 在Interface Builder中,您将TableView链接到IBOutlet变量。 You'll use this variable to interact with the TableView from the controller. 您将使用此变量与控制器中的TableView进行交互。

Any tutorial links or pertinent project source code would be much appreciated! 任何教程链接或相关的项目源代码将不胜感激!

When I started a similar project, I did not find a lot of good tutorials. 当我开始一个类似的项目时,我没有找到很多好的教程。 For me, Tekpub was the best resource for figuring out how to display manipulate data in an iPhone app. 对我而言, Tekpub是弄清楚如何在iPhone应用程序中显示操纵数据的最佳资源。

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

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