在Obj-C中,控制器复用一般可以这么写
.h文件中
typedef NS_ENUM(NSInteger, GapOrderType) {
GapOrderTypeUnderWay = 1, // 进行中
GapOrderTypeEnd,
GapOrderTypePay
};
- (instancetype)initWithType:(GapOrderType)type;
.m文件中
- (instancetype)initWithType:(GapOrderType)type{
if (self = [super init]) {
_type = type;
}
return self;
}
在Swift中写法
public enum Verify {
case underWay
case end
}
// 列表
class OwnerVerifyListController: BaseController {
fileprivate var type : Verify!
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
if type == Verify.underWay {
title = "控制器1"
}else{
title = "控制器2"
}
}
// 便利构造器
convenience init(type: Verify){
self.init()
self.type = type
}
}
用法示例,在父控制器中添加两个自控制器
fileprivate func addMyChild(){
let underVc = OwnerVerifyListController(type: Verify.underWay)
self.addChildViewController(underVc)
let endVc = OwnerVerifyListController(type: Verify.end)
self.addChildViewController(endVc)
}
配图示例
demo.png
网友评论