When should I use the UIViewController in IOS Programming?

I looked at the API for iOS programming and read about view controllers and UIViews. It seems that subclasses of UIViewController are really useful for Modal navigation and custom animation, but I see no other uses than this.

What is the advantage of using subclasses of UIViewController rather than the usual subclass of NSObject?

Why

@interface MyViewController : UIViewController { } -(void)handleEvent; @end 

Instead of just

 @interface MyViewController : NSObject { UIView* view; } @property(retain) UIView* view; -(void)handleEvent; @end 

Aren't you just adding the view to the window, and not the viewController itself? For most purposes, aren't all the functions you need to encapsulate in a UIView object? You just add it like this:

 [window addSubview:myViewControllerInstance.view] 

Is UIViewController used for use other than built-in functions such as Modal Navigation?

Thanks.

(Sorry if this is a stupid question, I have been studying this for 2 days already)

+6
ios objective-c uiviewcontroller uiview
source share
2 answers

Cocoa on Mac OS and iOS heavily uses the Model-View-Controller (MVC) pattern. Following the MVC model, the UIViewController is a controller class. Its task is to coordinate the interaction between your user interface (View objects) and your application data (model objects). Basically, the controller primarily places your application logic. It processes events and invokes the view and model, respectively.

See the UIViewController link , which has a good overview in the class.

Having received a UIViewController, you get a bunch of controller functions for free, for example, you download views from a Nib file (initWithNibName: bundle :) and respond to events related to viewing (viewWillAppear :, viewWillDisappear :). In addition, the UIViewController itself is a subclass of UIResponder that contains functionality for handling touch events (touchhesBegan: withEvent, for: Moved: withEvent, touchesEnded: withEvent).

Basically, there is no reason NOT to use the UIViewController with all the functions provided. Even if you manage to do this, it will be more efficient work, without real benefits.

+2
source share

Take a look at the properties and methods of an instance of the UIViewController class that you would not get for free if you just subclassed NSObject. It has a lot of everything that you will use in all your applications.

0
source share

All Articles