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 命名空间形式扩展的实现
#if os(macOS)
import AppKit
public typealias Image = NSImage
public typealias View = NSView
public typealias Color = NSColor
public typealias ImageView = NSImageView
public typealias Button = NSButton
#else
import UIKit
public typealias Image = UIImage
public typealias Color = UIColor
#if !os(watchOS)
public typealias ImageView = UIImageView
public typealias View = UIView
public typealias Button = UIButton
#else
import WatchKit
#endif
#endif
// MARK: -
// MARK: - MG
public struct MGNameSpaceWrappable<Base>:MGTypeWrapperProtocol {
public let base: Base
init(_ base: Base) {
self.base = base
}
}
public protocol MGTypeWrapperProtocol {
}
extension MGTypeWrapperProtocol {
public var mg: MGNameSpaceWrappable<Self> {
get {
return MGNameSpaceWrappable(self)
}
set {
}
}
}
// MARK: -
// MARK: - LYM
public protocol LYMNamespacePotocol {
associatedtype WrapperType
var lym: WrapperType { get }
static var lym: WrapperType.Type { get }
}
public extension LYMNamespacePotocol {
var lym: LYMNamespaceWrapper<Self> {
return LYMNamespaceWrapper(value: self)
}
static var lym: LYMNamespaceWrapper<Self>.Type {
return LYMNamespaceWrapper.self
}
}
public protocol LYMTypeWrapperProtocol {
associatedtype WrappedType
var wrappedValue: WrappedType { get }
init(value: WrappedType)
}
public struct LYMNamespaceWrapper<T>: LYMTypeWrapperProtocol {
public let wrappedValue: T
public init(value: T) {
self.wrappedValue = value
}
}