Swift 返回栈中特定的任意控制器(存在于navigationController.children)
extension UIViewController {
// 查找最合适的响应控制器
class func currentViewController() -> UIViewController? {
// Find best view controller
let viewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController
return UIViewController.findBestViewController(viewController)
}
class func findBestViewController(_ vc: UIViewController?) -> UIViewController? {
if vc?.presentedViewController != nil {
// Return presented view controller
return UIViewController.findBestViewController(vc?.presentedViewController)
} else if (vc is UISplitViewController) {
// Return right hand side
let svc = vc as? UISplitViewController
if (svc?.viewControllers.count ?? 0) > 0 {
return UIViewController.findBestViewController(svc?.viewControllers.last)
} else {
return vc
}
} else if (vc is UINavigationController) {
// Return top view
let svc = vc as? UINavigationController
if (svc?.viewControllers.count ?? 0) > 0 {
return UIViewController.findBestViewController(svc?.topViewController)
} else {
return vc
}
} else if (vc is UITabBarController) {
// Return visible view
let svc = vc as? UITabBarController
if (svc?.viewControllers?.count ?? 0) > 0 {
return UIViewController.findBestViewController(svc?.selectedViewController)
} else {
return vc
}
} else {
// Unknown view controller type, return last child view controller
return vc
}
}
}
/// MARk: - 返回到指定控制器
extension UIViewController {
/**
* 导航栏回到指定控制器
* className:控制器名字(字符串)
* animated:是否带动画
*/
func back(toController className: String!, animated: Bool) {
guard let appName: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String else {
return
}
let classStringName = appName + "." + className
guard let cls:AnyClass = NSClassFromString(classStringName) else {
return;
}
self.back(toControllerClass: cls, animated: animated)
}
/**
* 导航栏回到指定控制器
* cls:控制器类名)
* animated:是否带动画
*/
func back(toControllerClass cls: AnyClass, animated: Bool) {
if navigationController != nil, let childViewControllers = navigationController?.children {
let resluts = childViewControllers.filter { return $0.isKind(of: cls) }
if resluts.count>0,let firstObject = resluts.first {
navigationController!.popToViewController(firstObject, animated: animated)
}
}
}
}
使用截图,调用
Swift 命名空间形式扩展的实现
// MARK: -
// MARK: - LYM
public protocol NamespacePotocol {
associatedtype WrapperType
var lym: WrapperType { get }
static var lym: WrapperType.Type { get }
}
public extension NamespacePotocol {
var lym: NamespaceWrapper<Self> {
return NamespaceWrapper(value: self)
}
static var lym: NamespaceWrapper<Self>.Type {
return NamespaceWrapper.self
}
}
public protocol TypeWrapperProtocol {
associatedtype WrappedType
var wrappedValue: WrappedType { get }
init(value: WrappedType)
}
public struct NamespaceWrapper<T>: TypeWrapperProtocol {
public let wrappedValue: T
public init(value: T) {
self.wrappedValue = value
}
}
使用截图
调用
网友评论