Build the mainstream App framework in ten minutes

Build the mainstream App framework in ten minutes

[[147883]]

Build mainstream framework interface

Achievement

  • When we play with iPhone applications, have you noticed that most applications have a similar structure as shown above? The TabBar controller below can switch sub-controllers, and there is a Navigation bar above.
  • Our article mainly builds the framework of the main body, and the data has not been added yet

Analyze the basic process of doing projects

1. Build the main framework of the project

  • First build tabBarController (there is one below)
  • Then build NavigationController (there is one line above, and each sub-controller is different)

2. Think about the development approach

  • Storyboard construction (used when there are few interfaces)
  • Pure code construction (used when there are more than 5 interfaces, easy to manage, generally used in commercial projects)

Building a mainstream framework from scratch (pure code)

1. Preparation

  • Environment deployment

2. Initially build the basic interface

The first step is to design the directory (based on the modular + MVC concept, create basic file directories and files)

  • Create directory paths with modular ideas (usually create them in the real path first, then drag them into the project)
  • Customizing TabBarController

The second step is to put the code (set the window to start the root controller in AppDelegate.m)

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. // 1. Create a window  
  3. self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  4.  
  5. // 2. Set the root controller of the window  
  6. CYXTabBarController *tabBarVC = [[CYXTabBarController alloc]init];
  7. self.window.rootViewController = tabBarVC;
  8.  
  9. // 3. Display window  
  10. [self.window makeKeyAndVisible];
  11.  
  12. return YES;
  13. }

The third step is to create and add a subcontroller in CYXTabBarController.m

  1. - ( void )viewDidLoad {
  2. [ super viewDidLoad];
  3.  
  4. // 1. Add the first controller  
  5. // 1.1 Initialization  
  6. CYXOneViewController *oneVC = [[CYXOneViewController alloc]init];
  7.  
  8. // 1.2 Add oneVC as the root controller of UINavigationController  
  9. UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:oneVC];
  10.  
  11. // Set the title of the tabBar  
  12. nav1.title = @ "Home" ;
  13. [nav1.navigationBar setBackgroundImage:[UIImage imageNamed:@ "commentary_num_bg" ] forBarMetrics:UIBarMetricsDefault];
  14.  
  15. // Set the tabBar icon  
  16. nav1.tabBarItem.image = [UIImage imageNamed:@ "tab_home_icon" ];
  17.  
  18. // Set the title of navigationBar  
  19. oneVC.navigationItem.title = @ "Home" ;
  20.  
  21. // Set the background color (these operations can be done by each individual child controller)  
  22. oneVC.view.backgroundColor = [UIColor whiteColor];
  23.  
  24. // 1.3 Hand over UINavigationController to UITabBarController for management  
  25. [self addChildViewController:nav1];
  26.  
  27. // 2. Add the second controller  
  28. CYXTwoViewController *twoVC = [[CYXTwoViewController alloc]init];
  29. UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:twoVC];
  30. nav2.title = @ "Technology" ;
  31. nav2.tabBarItem.image = [UIImage imageNamed:@ "js" ];
  32. twoVC.navigationItem.title = @ "Technology" ;
  33. twoVC.view.backgroundColor = [UIColor blueColor];
  34. [self addChildViewController:nav2];
  35.  
  36. // 3. Add the third controller  
  37. CYXThreeViewController *threeVC = [[CYXThreeViewController alloc]init];
  38. UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:threeVC];
  39. nav3.title = @ "Blog Post" ;
  40. nav3.tabBarItem.image = [UIImage imageNamed:@ "qw" ];
  41. threeVC.navigationItem.title = @ "Blog Post" ;
  42. threeVC.view.backgroundColor = [UIColor yellowColor];
  43. [self addChildViewController:nav3];
  44.  
  45. // 4. Add the 4th controller  
  46. CYXFourViewController *fourVC = [[CYXFourViewController alloc]init];
  47. UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:fourVC];
  48. nav4.title = @ "My Jianghu" ;
  49. nav4.tabBarItem.image = [UIImage imageNamed:@ "user" ];
  50. fourVC.navigationItem.title = @ "My Jianghu" ;
  51. fourVC.view.backgroundColor = [UIColor grayColor];
  52. [self addChildViewController:nav4];
  53.  
  54. }

At this point, we have built the framework. Isn't it simple? The effect is as shown below:

But you may be tempted to complain that these are all redundant junk codes and unreadable. Let's extract the code below.

Step 4: Extract duplicate code

  • Since all the above codes are written in viewDidLoad and there are too many repeated codes, resulting in code redundancy and low scalability, let's make a preliminary optimization of the code.
  • Two methods are extracted here, one is the method to add all sub-controllers, and the other is the method to add each sub-controller
  1. - ( void )viewDidLoad {
  2. [ super viewDidLoad];
  3.  
  4. [self setUpAllChildViewController];
  5. }
  6.  
  7. /**
  8. * Add all sub-controller methods
  9. */  
  10. - ( void )setUpAllChildViewController{
  11. // 1. Add the first controller  
  12. CYXOneViewController *oneVC = [[CYXOneViewController alloc]init];
  13. [self setUpOneChildViewController:oneVC image:[UIImage imageNamed:@ "tab_home_icon" ] title:@ "Home" ];
  14.  
  15. // 2. Add the second controller  
  16. CYXTwoViewController *twoVC = [[CYXTwoViewController alloc]init];
  17. [self setUpOneChildViewController:twoVC image:[UIImage imageNamed:@ "js" ] title:@ "Technology" ];
  18.  
  19. // 3. Add the third controller  
  20. CYXThreeViewController *threeVC = [[CYXThreeViewController alloc]init];
  21. [self setUpOneChildViewController:threeVC image:[UIImage imageNamed:@ "qw" ] title:@ "Bowen" ];
  22.  
  23. // 4. Add the 4th controller  
  24. CYXFourViewController *fourVC = [[CYXFourViewController alloc]init];
  25. [self setUpOneChildViewController:fourVC image:[UIImage imageNamed:@ "user" ] title:@ "My Jianghu" ];
  26. }
  27.  
  28.  
  29. /**
  30. * Method to add a subcontroller
  31. */  
  32. - ( void )setUpOneChildViewController:(UIViewController *)viewController image:(UIImage *)image title:(NSString *)title{
  33.  
  34. UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:viewController];
  35. navC.title = title;
  36. navC.tabBarItem.image = image;
  37. [navC.navigationBar setBackgroundImage:[UIImage imageNamed:@ "commentary_num_bg" ] forBarMetrics:UIBarMetricsDefault];
  38.  
  39. viewController.navigationItem.title = title;
  40.  
  41. [self addChildViewController:navC];
  42. }

<<:  How was Overtime Wang made?

>>:  Four ways to update UI asynchronously on Android

Recommend

Is the promotion cost too high? You need to build a self-growth operating system

Recently, I have received a lot of inquiries abou...

China Canyon, so shocking!

Talking about the canyons in China In everyone...

Groundwater storage is not just about “storing water”

groundwater It is an important component of water...

What should you do when you try hard to fall asleep but can't?

The lives of modern people are becoming increasin...

Google advertising types and independent website operation skills

When it comes to off-site promotion, the first th...

How to prevent violations during live broadcasts?

Live streaming , using direct narration instead o...

How many airports are there in our country (updated in July 2020)?

New airports are built in my country every year, ...

Toutiao Wei Toutiao project, what content does Toutiao Wei Toutiao publish?

Wei Toutiao is a sub-function of Toutiao. It is r...

The most powerful review in history: Don’t panic if you have cockroaches at home!

Summer is here. Have some “cute little lives” app...

Headline delivery: What to do if the account volume is unstable?

1. The account volume is unstable and the planned...