Which controller to get data for UITableViews in IPhone app?
I have a CustomersViewController with a table view 开发者_运维知识库of customers. Click on a customer (didSelectRowAtIndexPath) and a OrdersViewController shows a table view of orders for that customer. I have test data in a NSArray to test with for now. However, at some point I'll need to do a call to a web service to get the order data. In which controller is it proper to make the call to the web service? In the didSelectRowAtIndexPath of the CustomersViewController or the viewWillAppear of the OrdersViewController? Any best practices and advice is much appreciated. Thanks.
Think about the nouns, not the verbs. A table view controller like you are describing displays a list of objects of a certain type. It should care most about that type. If you want info about another type of object, than another controller should handle that.
So CustomersViewController
sounds like it handles everything about customers, and OrdersViewController
sounds like it handles everything about orders.
So the OrdersViewController should handle fetching orders
- CustomersViewController fetches customers
- CustomersViewController displays customers
- User taps a customer
- OrdersViewController is created with a customer object or customer id passed to it during initialization
- OrdersViewController starts the request and displays a loading indicator
- OrdersViewController takes action when the request is complete by reloading the table and hiding the loading indicator
精彩评论