1.oc版
+ (instancetype)viewFromXib {
NSBundle* bundle = [NSBundle mainBundle];
NSString* className = NSStringFromClass([self class]);
return [[bundle loadNibNamed:className owner:nil options:nil] objectAtIndexSafe:0];
}
使用:
CustomView* view = [CustomView viewFromXib];
2.swift版
static func newInstance() -> LeftView? {
let objc = Bundle.main.loadNibNamed("LeftView", owner: nil, options: nil)
if let view = objc?.first as? LeftView {
return view
}
return nil
}
使用:
let view = CustomView.
newInstance()!
时间: 2024-10-12 13:05:50