For this kind of situation you need to think for the design of the app classes. I am giving you a very simple example that will make you think of an OOPs concept called as "Inheritance".
You can tweak the code to work with your header (i.e XIB)
I will create a base class btBaseViewController.h
& btBaseViewController.m
. This class will have the most common feature that I want all my viewcontrollers to have like
- A custom Header View with Button.
- A Content View.
- A method to renderUI.
- Another method to Enable / disable backButton.
In file btBaseViewController.h
make it like this.
#import <UIKit/UIKit.h>
@interface btBaseViewController : UIViewController
@property(nonatomic, retain) btHeader *headerView;
-(void)showHeaderViewWithBackButton:(BOOL)backButton;
@end
In btBaseViewController.m
implement the method as
-(void)showHeaderViewWithBackButton:(BOOL)backButton {
// Now implement this method in a way to show the backbutton or not depending on
[headerView.backButton setAlpha:backButton];
}
Now Let us say you have btHomeViewController
, derive it from btBaseViewController
#import "btBaseViewController.h"
@interface btHomeViewController : btBaseViewController
@end
In btHomeViewController.m
in viewdidload
write the code as
-(void)viewDidLoad {
if(self.navigationController.viewControllers.count >=2 ) {
// it means you have some thing on viewcontroller & you can go back
[self showHeaderViewWithBackButton:YES];
}
}
I have tried my best to project the solutions. You need to tweak it more.