简体   繁体   English

子类UIViewController或在非全屏视图时创建自定义NSObject

[英]Subclass UIViewController or create a custom NSObject when the view is not fullscreen

I need to create a class controller to manage the behavior of a custom view I created. 我需要创建一个类控制器来管理我创建的自定义视图的行为。 The standard approach is to subclass UIViewController, but in my case I instead decided to subclass the NSObject essentially for three reasons: 标准方法是将UIViewController子类化,但是在我的情况下,我决定主要是出于以下三个原因来子类化NSObject:

  • my view needs to be added as small subview of the main view controller (it will not be displayed using something like presentModalViewController or pushViewController...) and it does not require any kind of toolbar or navigation control inside of it 我的视图需要作为主视图控制器的小子视图添加(不会使用诸如presentModalViewController或pushViewController之类的东西显示),并且它不需要任何工具栏或导航控件。
  • Most probably my controller will not need to be notified for device orientation because its view will be always used in portrait format, so I'm not interested to receive the usual rotation messages willRotateToInterfaceOrientation etc... 很可能不需要通知我的控制器设备方向,因为它的视图将始终以纵向格式使用,因此我对接收通常的旋转消息willRotateToInterfaceOrientation等不感兴趣。
  • I need to keep this class as lightweight as possible minimizing memory consumption. 我需要使此类尽可能轻量,以最大程度地减少内存消耗。 Not subclassing UIViewController have the advantage to obtain a lighter class without a bunch of methods that I will never need to use 没有子类化UIViewController的优点是无需使用一堆我永远不需要使用的方法就可以得到一个轻量级的类。

The interface of my controller is pretty simple, example: 我的控制器的界面非常简单,例如:

@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

To display its internal view from the parent view controller, I will use something like this: 为了从父视图控制器显示其内部视图,我将使用类似以下的内容:

tabBarController = [[MyScrollTabBarController alloc] initWithPlace:aPlace];
tabBarController.viewState = MyScrollTabBarViewStateXX;
tabBarController.view.frame = CGRectMake(...); 
[self.view addSubview:tabBarController.view];

I'd like to know what do you think about my choice, if you think that there could be drawbacks in it and what do you usually do when you need to write a controller for a view which is not fullscreen like mine. 我想知道您对我的选择有何看法,如果您认为其中可能有缺点,以及需要为非全屏视图(如我的视图)编写控制器时通常要做什么。

Thanks 谢谢

I think this is an acceptable solution. 我认为这是可以接受的解决方案。

Another solution would be creating a "fat" view that does its controlling itself (like, for instance, MKMapView, UITextView etc.). 另一种解决方案是创建一个“胖”视图,该视图自己进行控制(例如,MKMapView,UITextView等)。 This might make things a little more manageable, and if the view is very specialized, and its controller is intended to only work with this one class of view, you don't really lose any reusability (because there isn't much). 这可能会使事情变得更易于管理,并且如果视图非常专业化,并且其控制器旨在仅使用这一类视图,则不会真正失去任何可重用性(因为没有太多可重用性)。

Yes, this is the correct approach. 是的,这是正确的方法。

UIViewControllers are specifically for controlling full-screen views, not for sub-screens. UIViewControllers特别用于控制全屏视图,而不是子屏幕。 In iOS5 there is a mechanism for composing sub-screen viewcontrollers in this way, but that's not available in iOS4 without lots of hackery. 在iOS5中,有一种以这种方式组成子屏幕视图控制器的机制,但是如果没有很多黑客,这在iOS4中是不可用的。

In cases where the view and controller are inherently coupled, you could also consider making a custom view subclass that is its own controller, so for example you could have a self-contained table view subclass that managed its own data and could just be dropped into a page. 如果视图和控制器本质上是耦合的,则还可以考虑创建一个自定义视图子类,该子类是其自己的控制器,例如,您可以拥有一个自包含的表视图子类来管理自己的数据,并且可以将其放入页面。

what do you usually do when you need to write a controller for a view which is not fullscreen like mine 当您需要为非全屏视图(如我的视图)编写控制器时,通常会做什么

It is not important that your view is not displayed full screen. 并非全屏显示视图也很重要。 It is possible (and usual) to have views consisting of subviews which each have their own controller. 可能(通常)具有由子视图组成的视图,每个子视图都有自己的控制器。

I need to keep this class as lightweight as possible minimizing memory consumption. 我需要使此类尽可能轻量,以最大程度地减少内存消耗。 Not subclassing UIViewController have the advantage to obtain a lighter class without a bunch of methods that I will never need to use 没有子类化UIViewController的优点是无需使用一堆我永远不需要使用的方法就可以得到一个轻量级的类。

Subclassing UIViewController does not consume an unreasonable amount of memory, so this should not be part of the consideration. 子类化UIViewController不会消耗不合理的内存量,因此这不应成为考虑的一部分。

[...] if you think that there could be drawbacks in it [...] [...]如果您认为其中可能存在弊端[...]

With your solution you loose flexibility. 使用您的解决方案,您失去了灵活性。 It is likely that you will reuse your solution in a context where you need to respond to UILifecyle-Messages or use other UIViewController features. 您可能会在需要响应UILifecyle-Messages或使用其他UIViewController功能的上下文中重用解决方案。

If your views shall be lightweight you could consider using a UIView subclass and use a delegate for the logic behind your view. 如果视图是轻量级的,则可以考虑使用UIView子类,并为视图背后的逻辑使用委托。

Hi You are subclassing NSObject and declaring a UIView inside it 嗨,您正在子类化NSObject并在其中声明一个UIView

@interface MyScrollTabBarController : NSObject <MyTabBarViewDelegate> { }

@property (nonatomic, readonly) UIView *view;

I Suggest you should subclass UIView, so you will not have to declare an additional view object. 我建议您应该继承UIView,这样就不必声明其他视图对象。

so instead of self.view you can simply refer as self 因此, 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.

相关问题 何时将UIViewController子类化为自定义子视图? - When to subclass UIViewController for custom subview? 我可以创建一个继承自UIViewController的自定义子类的UITableViewController吗? - Can I create a UITableViewController that inherits from a custom subclass of UIViewController? Objective-C –将数据从NSObject子类发送到UIViewController - Objective-C – Sending data from NSObject subclass to an UIViewController 调用presentModalViewController时NSObject中的UIViewController不显示 - UIViewController in NSObject not presenting when calling presentModalViewController 自定义UIView子类作为UIViewController属性 - Custom UIView subclass as a UIViewController property UIView或UIViewController用于自定义视图 - UIView or UIViewController for custom View 使用UIViewController子类自定义iPhone控件 - Custom iPhone control using UIViewController subclass 如何用.xib快速创建UIViewController子类? - How to create a UIViewController subclass with .xib in swift? 从NSObject类到UIViewController类获取NSMutableArray时的内存管理 - Memorymanagement when getting a NSMutableArray from NSObject class to UIViewController class 当视图包含具有自定义图像的UIButton时,UIViewController encodeWithCoder失败 - UIViewController encodeWithCoder fails when view contains a UIButton with custom image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM