简化控制器内容

The first rule of thumb for refactoring is to revisit your code to check whether the if statements you’re using are truly for a logical branching and not for class-based switching. (In the cellForRowAtIndexPath: method).

The second rule is to check if you are using an if condition to branch code for different kinds of tables, such as in the cellForRowAtIndexPath: method. As I showed you previously, code like this should be refactored and solved elegantly using object-oriented techniques. Every class-based switching like this can be solved in an object-oriented way. This refactoring technique holds good for any language, not just Objective-C.

Adhering to these two refactoring techniques will reduce much of the code in your controller class.

Remember that your controller should act as a mediator among your models and UI elements defined at that level and not at the subclass level. In other words, a view controller can set the property of a UI element defined in its scope but not that of a UI element that is inside a subclass. For example,

 self.textLabel.text = NSLocalizedString(@”Hello”, @””) 

is okay, but

self.customView.textLabel.text = NSLocalizedString(@”Hello”, @””)
should be avoided. The recommended way is to move this code into the customView’s class.

你可能感兴趣的:(简化控制器内容)