我想用xib文件實現繼承。是的,有點奇怪,但讓我告訴你爲什麼。何時設置XIB插座屬性?
我有一個類,SLBaseViewController,我的許多視圖控制器繼承自。當我想要一個兒童視圖控制器時,我用通常的方式創建它:
SLHomeViewController *controller = [[SLHomeViewController alloc] initWithNibName:@"SLHomeViewController" bundle:nil];
這工作正常。 SLHomeViewController是一個SLBaseViewController(它是一個UIViewController)。
我這樣做是因爲我有其他視圖控制器,我想繼承SLBaseViewController行爲。就我而言,我有一個在我的應用程序中很常見的導航UI小部件,因此SLSceneViewControll也從SLBaseViewController繼承,並且SLHomeViewController和SLSceneViewController都獲得了自定義導航小部件的行爲。
自定義導航窗口小部件還具有在SLBaseViewControllers中通用的位置信息。所以我實現了一個窮人的xib繼承方式。
@interface SLBaseViewController : UIViewController <SLNavBarViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UIView *navBarExtendedFPO;
,如果我創建一個SLHomeViewController是加載SLBaseViewController的廈門國際銀行,然後將複製它有趣的屬性繼承在initWithNibName
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
Class callingClass = [self class];
Class slBaseViewControllerClass = NSClassFromString (SL_BASE_VC_CLASS_NAME);
if (callingClass != slBaseViewControllerClass) {
SLBaseViewController *controller = [[SLBaseViewController alloc] initWithNibName:@"SLBaseViewController" bundle:nil];
// now load all the properties by hand
self.navBarExtendedFPO = controller.navBarExtendedFPO;
}
}
return self;
}
完成。如果initWithNibName檢測到它正在加載一個SLBaseViewController,它什麼都不做,從而防止了無限循環。
問題是,當然,出口屬性尚未設置。所以它只是複製零。
那麼這些出口屬性何時設置?
或者 - 有沒有更好的方法來做我想做的事情?這一切似乎都是美好的,直到我手工複製這些屬性。這對我來說似乎很脆弱。
(請注意,我很好iOS6的,唯一的解決辦法。)
好了,所以你有兩個'UIViewControllers'從第三'UIViewController'繼承,正確?這裏的問題是,你想在所有'UIViewController's' + UIViewController's' XIB中使用一個小部件,通常從你的子類中使用它?是嗎? – Peres 2013-04-11 14:45:58
在視圖控制器中存在惰性初始化。所以,只有在調用屬性controller.view之後纔會加載視圖 - 在viewController中將調用loadView方法和viewDidLoad方法。 – BergP 2013-04-11 14:46:04
是的,傑基男孩,除了可能會有超過2的方式。 – 2013-04-11 14:48:15