Subclass UIViewController or create a custom NSObject if the view is not full-screen

I need to create a class controller to control the behavior of the user view that I created. The standard approach is to subclass the UIViewController, but in my case, I instead decided to subclass the NSObject for essentially three reasons:

  • My view should be added as a small submatrix of the main view controller (it will not be displayed using something like presentModalViewController or pushViewController ...), and it does not require any toolbar or navigation control inside it
  • Most likely, my controller will not need to be notified about the orientation of the device, because its representation will always be used in portrait format, so I'm not interested in receiving regular rotation messages. willRotateToInterfaceOrientation etc.
  • I need this class to be as light as possible to minimize memory consumption. Not subclassing the UIViewController has the advantage of getting a lighter class without a bunch of methods that I will never need to use

The interface of my controller is quite simple, for 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 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 would like to know what you think of my choice if you think that it may be flawed and what you usually do when you need to write a controller for a view that is not full-screen like mine.

thanks

+4
source share
4 answers

Yes, this is the right approach.

UIViewControllers are designed specifically for managing full-screen views, and not for sub-screens. IOS5 has a mechanism for creating sub-screens with viewing the screen in this way, but not available in iOS4 without a lot of hackers.

In cases where the view and the controller are interconnected in nature, you can also consider creating a custom subclass of the view, which is its own controller, so for example, you can have a standalone subclass of the table view that manages its own data and can just be lowered to page.

+1
source

I think this is an acceptable solution.

Another solution would be to create a live view that will control itself (for example, MKMapView, UITextView, etc.). This can make things a little more manageable, and if the presentation is very specialized, and its controller is designed only for working with this class, you really do not lose the possibility of reuse (because there are not so many).

+1
source

what do you usually do when you need to write a controller for a view that is not fullscreen like mine.

It doesn’t matter that your view is not displayed in full screen. It is possible (and usually) to have representations consisting of subzones, each of which has its own controller.

I need this class to be as light as possible to minimize memory consumption. Non-subclasses of the UIViewController have the advantage of getting a lighter class without a bunch of methods that I will never need to use

Subclassing the UIViewController does not consume unreasonable memory, so this should not be part of the consideration.

[...] if you think that it may be flawed [...]

Thanks to your decision, you get flexibility. You are probably reusing your solution in a context where you need to respond to UILifecyle-Messages or use other functions of the UIViewController.

If your views should be lightweight, you can consider using a subclass of UIView and use a delegate to logic your view.

0
source

Hi, you subclass NSObject and declare a UIView inside it.

 @interface MyScrollTabBarController : NSObject <MyTabBarViewDelegate> { } @property (nonatomic, readonly) UIView *view; 

I suggest you subclass UIView, so you don't have to declare an additional view object.

so instead of self.view you can just call self

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

Source: https://habr.com/ru/post/1391816/


All Articles