對於這種情況,您需要考慮設計應用程序類。我給你一個非常簡單的例子,它會讓你想到一個叫做「繼承」的OOP概念。
你可以調整代碼來處理你的頭(我。e XIB)
我將創建一個基類btBaseViewController.h
& btBaseViewController.m
。這個類將具有最常見的功能,我希望我所有的視圖控制器都有這樣的效果。
- 帶按鈕的自定義標題視圖。
- 內容視圖。
- renderUI的一種方法。
- 另一種啓用/禁用backButton的方法。
在文件btBaseViewController.h
使它像這樣。
#import <UIKit/UIKit.h>
@interface btBaseViewController : UIViewController
@property(nonatomic, retain) btHeader *headerView;
-(void)showHeaderViewWithBackButton:(BOOL)backButton;
@end
在btBaseViewController.m
實現方法
-(void)showHeaderViewWithBackButton:(BOOL)backButton {
// Now implement this method in a way to show the backbutton or not depending on
[headerView.backButton setAlpha:backButton];
}
現在讓我們假設你有btHomeViewController
,獲得從btBaseViewController
#import "btBaseViewController.h"
@interface btHomeViewController : btBaseViewController
@end
在在viewdidload
寫代碼爲
-(void)viewDidLoad {
if(self.navigationController.viewControllers.count >=2) {
// it means you have some thing on viewcontroller & you can go back
[self showHeaderViewWithBackButton:YES];
}
}
我盡我所能來投影解決方案。你需要更多地調整它。