E
E
Era2012-07-05 20:16:00
Objective-C
Era, 2012-07-05 20:16:00

How to avoid code duplication in two base classes inherited from UIViewController and UITableViewController?

Essence of the question:
I have two base controllers:
1.BaseController inherited from UIViewController
2.TableBaseController inherited from UITableViewController
It so happened that these base controllers have methods that mirror each other
Example:
- (void)loadView;
- (void)viewDidLoad;
Now I'm puzzling over how to remove the duplication of logic and make the code beautiful.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
No_Time, 2012-07-05
@No_Time

Something is not clear. Do you want to make one load method for 2 different controllers? If so, then that's nonsense. Or I misunderstood you?
So for reference: there is also viewDidAppear, viewWillAppear, which, according to your logic, also almost repeat each other.

P
Pilot34, 2012-07-20
@Pilot34

Was also confused with this issue. The easiest way is not to inherit from UITableViewController, but to make UITableView in a regular controller using xib. There won't be much difference.

P
pestrov, 2012-07-24
@pestrov

You can simply create a category on the NavigationBar, adding full customization methods to it - adding a background, buttons, sliders, your own for each controller. Then in - (void)viewDidLoad you can simply call the method that customizes the NavigationBar for this controller.
The call will look something like this:

- (void)viewDidLoad {
[self.navigationController.navigationBar setBarForSmth];
}

The category itself will be the following:
@interface UINavigationBar (NavigationBar)
-(void)setBarForCard;
@end


@implementation UINavigationBar (NavigationBar)

- (void)setBarForSmth
{

    UIImageView *aTabBarBackground = ;
    aTabBarBackground.frame = CGRectMake(0,0,self.frame.size.width,44);
    [self sendSubviewToBack:aTabBarBackground];
//Ну и тут добавляеть контроллов по вкусу

You may often need to do this in viewWillAppear rather than viewDidLoad, so it won't be called when switching between controllers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question