oc
通过当前视图获取父视图的控制器
- (UIViewController *)LCZGetSuperViewController:(UIView *)view
{
for (UIView* next = [view superview]; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[HomePageViewController class]]) {
return (HomePageViewController *)nextResponder;
}
}
return nil;
}
swift
/// 通过当前视图获取父视图的控制器
///
/// - Parameters:
/// - currentView: 当前视图
/// - viewController: 需要获取到的控制器
/// - Returns: 返回控制器
func LCZGetSuperViewController<T: UIViewController>(currentView:UIView, viewController: T.Type) -> T? {
var view: UIView = currentView.superview!
while view.next!.isKind(of: T.self) != true {
guard view.superview != nil else {
return nil
}
view = view.superview!
}
return view.next as? T
}
/// 获取当前视图中的目标父视图
///
/// - Parameters:
/// - currentView: 当前视图
/// - superView: 目标父视图
/// - Returns: 返回父视图
public func LCZGetSuperView<T: UIView>(currentView: UIView, superView: T.Type) -> T? {
var view: UIView = currentView.superview!
while view.isKind(of: T.self) != true {
guard view.superview != nil else {
return nil
}
view = view.superview!
}
return view as? T
}
网友评论