iOS in UIViewController perfect slimming

For UIViewControllerquestions downsizing is a cliché, but now they have more infrastructure to achieve this effect, such as MVVM and so on. But this time we are the traditional MVC architecture to achieve the perfect weight-loss. This method is also not completely preclude the use of other methods like thin MVVM on this basis.

scene

First, let us look at what we are going interface effects


6644906-a62f73002e0751fb.jpeg

The interface is not hard to look at, the uppermost two-dimensional code scanning; intermediate to enter some information, figures 1,2,3 network request is a button click and then select the pop-up interface for users to choose; most below historical data.

In fact, write down interact very much, very multi-interface, business logic is very complex,
traditional MVC down the entire .h file estimate at least 1500 lines.

Weight loss ideas

Achieved through two red figure above, we have this interface is divided into three parts. The statement four UIViewController:
namely:

  • ScanViewController Scan function
  • InfoInputViewControllerEnter information
  • HistoryViewControllerhistorical data
  • MyViewControllerAssembly features
    in MyViewControllerwe assembled, and to deal with some non-business events
self.myScanVC = [[ScanViewController alloc] init];
[self addChildViewController:self.myScanVC];
[self.view addSubview:self.myScanVC.view];
[self.myScanVC didMoveToParentViewController:self];

self.myInforInputVC = [[InfoInputViewController alloc] init];
[self addChildViewController:self.myInforInputVC];
[self.view addSubview:self.myInforInputVC.view];
[self.myInforInputVC didMoveToParentViewController:self];

self.myHistoryVC = [[HistoryViewController alloc] init];
[self addChildViewController:self.myHistoryVC];
[self.view addSubview:self.myHistoryVC.view];
[self.myHistoryVC didMoveToParentViewController:self];

By this method, the first step we have successfully reduced the degree of coupling, but also in a larger project, there is the advantage ScanViewControllerand HistoryViewControllertwo classes can achieve the effect of reuse, other features also need these two functions , all business all logic in InfoInputViewControllerthe


Because InfoInputViewControlleralso has a lot of business logic and click the Start button to after the main response.
Now we look at how simple click event:

- (IBAction)actionClick:(UIButton *)sender {
    [self.view endEditing:YES];
    [self.actionsContext doActionWithLevel:sender.tag];
}

The judgment is no, and we put all the business logic is encapsulated in a one of Action.
Afternoon is the design of our class structure


6644906-bc298bc89f5a7431.png

In fact, I have more design ideas alone wrote "insights and practical design pattern (b)"
specific code article also.

This method not only eliminates the determination if ... else while clicking each time the business logic will trip individual packages.


By combining these two methods will be successful "UIViewController" thin simply can not be lost! !

Reproduced in: https: //www.jianshu.com/p/a678defae5b5

Guess you like

Origin blog.csdn.net/weixin_34304013/article/details/91079553