[英]Subclass UIViewController or create a custom NSObject when the view is not fullscreen
我需要創建一個類控制器來管理我創建的自定義視圖的行為。 標准方法是將UIViewController子類化,但是在我的情況下,我決定主要是出於以下三個原因來子類化NSObject:
我的控制器的界面非常簡單,例如:
@interface MyScrollTabBarController : NSObject <MyTabBarViewDelegate> { }
/**
* The view is created internally by the controller and the client class
* can access to it in readonly mode
*/
@property (nonatomic, readonly) UIView *view;
/**
* A Property to change the view appearance
*/
@property (nonatomic, assign) MyScrollTabBarViewState viewState;
/**
* Others properties used to construct the view's subviews
*/
@property (nonatomic, retain) Location *rootLocation;
@property (nonatomic, readonly, retain) Place *place;
/**
* Designated initializer
*/
- (id)initWithPlace:(Place *)aPlace;
- (void)setRootLocation:(Location *)location animated:(BOOL)animated;
@end
為了從父視圖控制器顯示其內部視圖,我將使用類似以下的內容:
tabBarController = [[MyScrollTabBarController alloc] initWithPlace:aPlace];
tabBarController.viewState = MyScrollTabBarViewStateXX;
tabBarController.view.frame = CGRectMake(...);
[self.view addSubview:tabBarController.view];
我想知道您對我的選擇有何看法,如果您認為其中可能有缺點,以及需要為非全屏視圖(如我的視圖)編寫控制器時通常要做什么。
謝謝
我認為這是可以接受的解決方案。
另一種解決方案是創建一個“胖”視圖,該視圖自己進行控制(例如,MKMapView,UITextView等)。 這可能會使事情變得更易於管理,並且如果視圖非常專業化,並且其控制器旨在僅使用這一類視圖,則不會真正失去任何可重用性(因為沒有太多可重用性)。
是的,這是正確的方法。
UIViewControllers特別用於控制全屏視圖,而不是子屏幕。 在iOS5中,有一種以這種方式組成子屏幕視圖控制器的機制,但是如果沒有很多黑客,這在iOS4中是不可用的。
如果視圖和控制器本質上是耦合的,則還可以考慮創建一個自定義視圖子類,該子類是其自己的控制器,例如,您可以擁有一個自包含的表視圖子類來管理自己的數據,並且可以將其放入頁面。
當您需要為非全屏視圖(如我的視圖)編寫控制器時,通常會做什么
並非全屏顯示視圖也很重要。 可能(通常)具有由子視圖組成的視圖,每個子視圖都有自己的控制器。
我需要使此類盡可能輕量,以最大程度地減少內存消耗。 沒有子類化UIViewController的優點是無需使用一堆我永遠不需要使用的方法就可以得到一個輕量級的類。
子類化UIViewController不會消耗不合理的內存量,因此這不應成為考慮的一部分。
[...]如果您認為其中可能存在弊端[...]
使用您的解決方案,您失去了靈活性。 您可能會在需要響應UILifecyle-Messages或使用其他UIViewController功能的上下文中重用解決方案。
如果視圖是輕量級的,則可以考慮使用UIView子類,並為視圖背后的邏輯使用委托。
嗨,您正在子類化NSObject並在其中聲明一個UIView
@interface MyScrollTabBarController : NSObject <MyTabBarViewDelegate> { }
@property (nonatomic, readonly) UIView *view;
我建議您應該繼承UIView,這樣就不必聲明其他視圖對象。
因此, self.view
您可以簡單地稱為self
tabBarController = [[MyScrollTabBarController alloc] initWithPlace:aPlace];
tabBarController.viewState = MyScrollTabBarViewStateXX;
tabBarController.frame = CGRectMake(...);
[self.view addSubview:tabBarController];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.