views:

490

answers:

3

If I have a view with several buttons, a table, and some other controls, do I need a controller for each type (button, table, etc), or should I have one controller per view that handles all of the necessary actions? It doesn't seem a single controller is possible as they may have to inherit from different parent classes. What is the best method?

+1  A: 

You can use one controller for all those. Put all your actions in the controller and hook up the buttons by declaring IBActions and linking them in Interface Builder. The first few chapters of Cocoa Programming for Mac OS X by Aaron Hillegass explains this really well.

If you have a UITableView, you have to remember to set up the UITableViewDelegate and make sure that you implement the methods it needs:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

For the others, set up IBOutlets in your controller and hook them up in Interface Builder. Depending on the controls you're using, you may need other delegates (for example a text field delegate).

nevan
+1  A: 

Excellent response, nevan. Also, the Cocoa (Touch) APIs make heavy use of delegates. This enables you to use a single controller for multiple things without having to subclass everything. The controller then is a delegate for several components (such as a UITableView).

Chris Eidhof
A: 

Can I use one controller to have a Tableview (bottom half of the screen) shared with a Pickerview (top half of the screen)?

Also, is the Tableview the best way to implement an eight row by two column view used for display purposes only or, is there a better - easier - way?

TrueScot
If you posted this as a question, and not an answer you'd get more responses.Tableviews aren't great at showing more than one column of information.
mahboudz