iOS Page Display and Logic: MVC Design Pattern and ViewController Management
The article explains how iOS uses the MVC pattern and ViewController hierarchy—distinguishing content and container controllers such as UINavigationController—to manage page display, navigation stacks, custom transitions, state restoration, and memory handling, enabling complex multi‑level navigation and functional implementation.
This article explains iOS page display and logic, focusing on the MVC design pattern and ViewController management.
It describes how Model, View, and Controller interact: View displays data defined by Model; View logic is controlled by Controller; Controller also initializes Model and passes its data to View.
User actions in View are communicated via Controller to create or update Model; when Model changes (e.g., via network), it notifies Controller, which updates the corresponding View.
iOS provides two categories of ViewController: content-display types (e.g., UIViewController, UITableViewController) and container types (e.g., UINavigationController, UITabBarController). Container ViewControllers manage an array of child ViewControllers and inherit from UIViewController.
The article details UINavigationController as a stack-based container: it maintains an ordered array (navigation stack) where the root view controller is at the bottom and the newest at the top. Navigation is performed by pushViewController (to add) and popViewController (to remove), with additional methods popToViewController and popToRootViewController for specific jumps, optionally animated.
UINavigationController* nav = [[UINavigationController alloc] init]; // 新建两个ViewController,并设置他们的View的背景颜色 UIViewController* vc1 = [[UINavigationController alloc] init]; vc1.view.backgroundColor = [UIColor redColor]; UIViewController* vc2 = [[UINavigationController alloc] init]; vc2.view.backgroundColor = [UIColor blueColor]; [nav pushViewController:vc1 animated:NO]; //把vc1推到nav的stack中 [nav pushViewController:vc2 animated:NO]; //把vc2推到nav的stack中 UIViewController* top = nav.topViewController; //这时top其实就是vc2 [nav popViewControllerAnimated:NO]; //这时nav的顶层VC被pop出,top变成vc1
UINavigationController also manages the navigation bar and toolbar visibility.
//承接上面的代码.. [nav popToViewController:vc1 animated:NO]; [nav popToRootViewControllerAnimated:NO];
Custom transition animations can be implemented using UIViewController’s transitionFromViewController:toViewController:duration:options:animations:completion: method, allowing developers to specify duration and animation options such as flip from right.
[vc3 transitionFromViewController:vc2 toViewController:vc1 duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{} completion:nil];
State saving and restoration is supported via the UIStateRestoring protocol and restorationIdentifier property, enabling ViewControllers to encode their state before the app goes to background; custom container ViewControllers must assign unique restorationIdentifiers to child view controllers.
Memory management relies on didReceiveMemoryWarning to release unused resources; excessive ViewControllers can lead to out-of-memory crashes, with example limits given for iPhone XS Max (total usable memory ~3735 MB, per‑app limit ~2039 MB).
The article concludes that multi‑level page management is handled by ViewController through various classifications, enabling complex navigation and functional implementation.
Tencent Music Tech Team
Public account of Tencent Music's development team, focusing on technology sharing and communication.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.