0

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?

2 Answers 2

1

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

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.