开发者

Seeking advice for iPhone app structure

开发者 https://www.devze.com 2023-01-09 16:07 出处:网络
I\'ve been tasked with creating an iPhone app which will tap into a series of REST API JSON feeds from Twitter.

I've been tasked with creating an iPhone app which will tap into a series of REST API JSON feeds from Twitter.

I'm somewhat new to iPhone development, but I've created a few apps before, 开发者_如何学Gothough none of them required tapping into API data.

So let's say we have 3 views in Interface Builder that are being controlled by a tabcontroller:

  • Feed
  • Followers
  • Following

Each of these views need to display a JSON feed in a tableview. 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.

I've installed and imported the json-interface library in my project. I have also successfully received the feed in an 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? All the tutorials I see only show one JSON request being formatted into one table on one view. For instance: http://iosdevelopertips.com/cocoa/json-framework-for-iphone-part-2.html

  • How does Interface Builder fit into this? How can I use the tables I inserted into my views in 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. 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.

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.

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. 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.


How should I prep my application to make requests to multiple JSON feeds and display the respective data in different tables on different views? ...

Each ViewController will take care of their own JSON requests like the tutorial.

How does Interface Builder fit into this? How can I use the tables I inserted into my views in Interface Builder?

You create a IBOutlet for your tableviews in your ViewControllers. For example:

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

In Interface Builder you link your TableView to the IBOutlet variable. You'll use this variable to interact with the TableView from the controller.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消