gpt4 book ai didi

objective-c - 将委托(delegate)移至他们自己的类(class)?

转载 作者:行者123 更新时间:2023-12-02 21:53:40 25 4
gpt4 key购买 nike

我的 View Controller 对我来说有点大了。我正在实现五个委托(delegate)协议(protocol),并准备添加第六个。

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

用一个 Controller 来实现所有这些似乎很荒谬,但它们并没有在其他地方使用。这些应该在自己的类中还是在 View Controller 中?

最佳答案

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

<强>1。将 ABCViewController.m 中的所有声明移至 ABCViewController.h 中的私有(private)类别

// 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

关于objective-c - 将委托(delegate)移至他们自己的类(class)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11054265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com