我的视图控制器对我来说有点大。我正在实现五个委托协议,并打算添加第六个。

ABCViewController : UITableViewController<NSFetchedResultsControllerDelegate,
                                          UITableViewDelegate,
                                          UITableViewDataSource,
                                          UIAlertViewDelegate,
                                          CLLocationManagerDelegate>


一个实现所有这些功能的控制器似乎很荒谬,但是没有在其他任何地方使用它们。这些应该在自己的类中还是在视图控制器中?

最佳答案

您可以向ABCViewController添加类别,如下所示:

1.将ABCViewController.m中的所有声明移到ABCViewController.h中的私有类别中

// in ABCViewController.h
@interface ABCViewController : UIViewController <delegates>
// anything that's in the _public_ interface of this class.
@end

@interface ABCViewController ()
// anything that's _private_ to this class.  Anything you had defined in the .m previously
@end


2. ABCViewController.m应包含该.h。

3.然后在ABCViewController + SomeDelegate.h和.m中

// in ABCViewController+SomeDelegate.h
@interface ABCViewController (SomeDelegateMethods)

@end

// in ABCViewController+SomeDelegate.m
#import "ABCViewController+SomeDelegate.h"
#import "ABCViewController.h"  // here's how will get access to the private implementation, like the _fetchedResultsController

@implementation ABCViewController (SomeDelegateMethods)

// yada yada

@end

07-24 09:36