如题, 在项目中,有一个基类BaseViewController 继承于系统的UIViewController,然后我的controller都继承于BaseViewController, 但是我项目中还有很多UITableViewController,怎么让TableViewController也拥有BaseViewController中的方法..
如题, 在项目中,有一个基类BaseViewController 继承于系统的UIViewController,然后我的controller都继承于BaseViewController, 但是我项目中还有很多UITableViewController,怎么让TableViewController也拥有BaseViewController中的方法..
利用Runtime方法在UIViewController上添加需要的属性和方法,重写方法用保存后替换的方式实现。
但要考虑到是否有不能继承这些修改的类,比如UINavigationController。
用类别的方式,给UIViewController加方法
如果用重构,想了想比较麻烦,需要抽提一个中介类,然后BaseViewController和BaseTableViewController需要一个中介对象进行调用。麻烦之处在于要把方法和成员提出去,并且原来代码里所有的self,替换成self.xxx以访问原来的self指针。
建议可以不用UITableViewController
:)实际工程中,感觉没有什么优势……
一般的做法是给UIViewController
里面 addSubView 加一个UITableView
,delegate
和dataSource
设成self
就行了。
你可以试试直接把你的UITableViewController
改成BaseNameController
,之前写好的代码都不用动~
UIViewController
实现了UITableViewDataSource
和UITableViewDelegate
协议以后就和UITableViewController
没什么区别。
让你自己的VC继承BaseViewController
,实现这两个协议,再把你的UITableView
的delegate
和dataSource
都设为这个VC就行了。
2 回答1k 阅读
1 回答1.1k 阅读✓ 已解决
1 回答2.7k 阅读
1 回答1.5k 阅读
1 回答1.4k 阅读
1.7k 阅读
1 回答1.1k 阅读
建一个BaseTableViewController继承与BaseViewController.在BaseTableViewController中声明一个属性UITableView,配好tableView的基本属性了就行了。然后你用UITableViewController时,就再建一个VC继承与BaseTableViewController就行了。