2017-04-19 25 views
0

我有以下代码:如何调用扩展方法在overrideViewDidLoad在迅速

extension ViewController { 


func AddLeftGesture(){ 
    let SwipeLeft:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyDismissOnSwipeLeft)) 

    self.view.addGestureRecognizer(SwipeLeft) 
} 

func MyDismissOnSwipeLeft(){ 

    self.dismiss(animated: true, completion: nil) 
} 

和我想完成的是覆盖viewDidLoad中和 呼叫AddLeftGesture方法,这样它会成为其中的一部分每个VC我做 ,我不必一次又一次地在每个viewDidLoad,

这是可能的吗?或者你们有其他建议吗?

+0

如果'ViewController'是你自己的类,为什么不直接在类定义中定义呢? – kennytm

回答

1

以及我不认为这是一个好主意,因为通常viewDidLoad用于设置大多数属性,如果你想在视图控制器中重写它,你应该再次写它。我可以建议的是,基地ViewController并在该viewDidLoad只要你想改变什么添加以下代码,然后从基本视图控制器每个子类的viewController,这样你只需要调用super.viewDidLoad

class BaseViewController: UIViewController{ 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     addLeftGesture() 
    } 
} 


class CustomViewController: BaseViewController{ 

} 
0

有两个选项AFAIK。要么你可以继承UIViewController,然后让所有的控制器都继承自子类,否则你可以调试UIViewControllerviewDidLoad()

我个人会选择swizzling,尽管它有一个缺点 - 它隐藏了实现,并且可能会让新开发人员进入项目时感到困惑。因此,请确保您在项目自述文件和代码中的某处正确记录此文件。

现在对于一些代码示例:

子类UIViewController

MyViewController.swift

class MyViewController: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     addGesture() 
    } 

    func addGesture() { 
     // Do what you need 
    } 
} 

class OtherViewController: MyViewController { 
    // Automatically will add gesture because it's a subclass of MyViewController 
} 

混写viewDidLoad

什么方法混写所做的是,它excha你的方法的实现。这只是意味着你的函数的名字来自不同函数的代码。有关此主题read this article的更多信息。

UIViewController+Swizzle.swift

static func swizzle(selector originalSelector: Selector, 
        with newSelector: Selector, 
        on targetClass: AnyClass) { 
    let originalMethod = class_getInstanceMethod(targetClass, originalSelector) 
    let swizzledMethod = class_getInstanceMethod(targetClass, newSelector) 

    // If we were able to add the swizzled function, replace methods. 
    // Otherwise exchange implementations if method already exists. 
    if class_addMethod(targetClass, originalSelector, 
         method_getImplementation(swizzledMethod), 
         method_getTypeEncoding(swizzledMethod)) { 

     class_replaceMethod(targetClass, newSelector, 
          method_getImplementation(originalMethod), 
          method_getTypeEncoding(originalMethod)) 
    } else { 
     method_exchangeImplementations(originalMethod, swizzledMethod) 
    } 
} 

extension UIViewController { 

    // This function is getting called automatically by the runtime, 
    // when this class is loaded to perform some additional intiialization. 
    // However, this has now been deprecated in Swift, so only option is to 
    // declare a static function which you need to remember to call from 
    // somewhere, preferably early in your app initialization, like your 
    // didFinishLaunching function in AppDelegate or even AppDelegate's init 
    // function. I kept the initialize function in the code as a reference, 
    // however you would probably want to write it like in the comment 
    // below, to silence the warning. 
    // 
    // class func swizzle() 
    // 
    open override class func initialize() { 
     if self != UIViewController.self { return } 

     let swizzlingClosure:() = { 
      swizzle(selector: #selector(UIViewController.viewDidLoad), 
          with: #selector(UIViewController.swizzled_viewDidLoad), 
          on: UIViewController.self) 
     }() 

     swizzlingClosure 
    } 

    @objc private func swizzled_viewDidLoad() { 
     // Calls the original implementation, 
     // because implementations are switched. 
     swizzled_viewWillAppear(animated) 

     // Do whatever you need 
     addGesture() 
    } 

    @objc func addGesture() { 
     // Add your gesture 
    } 
} 
0

产生这个类它继承UITapGestureRecognizer

open class BlockTap: UITapGestureRecognizer { 
    fileprivate var tapAction: ((UITapGestureRecognizer) -> Void)? 

public override init(target: Any?, action: Selector?) { 
    super.init(target: target, action: action) 
} 

public convenience init (
    tapCount: Int = 1, 
    fingerCount: Int = 1, 
    action: ((UITapGestureRecognizer) -> Void)?) { 
     self.init() 
     self.numberOfTapsRequired = tapCount 

     #if os(iOS) 
     self.numberOfTouchesRequired = fingerCount 
     #endif 

     self.tapAction = action 
     self.addTarget(self, action: #selector(BlockTap.didTap(_:))) 
} 

open func didTap (_ tap: UITapGestureRecognizer) { 
    tapAction? (tap) 
} 
} 

然后作出的UIView的延伸

extension UIView { 
     public func addTapGesture(tapNumber: Int = 1, action: ((UITapGestureRecognizer) ->())?) { 
      let tap = BlockTap(tapCount: tapNumber, fingerCount: 1, action: action) 
      addGestureRecognizer(tap) 
      isUserInteractionEnabled = true 
     } 
    } 

那么你可以使用它作为

override func viewDidLoad() { 
     super.viewDidLoad() 
     self.view.addTapGesture(action: {[unowned self] (_) in 
       //Do whatever on click of View 
     }) 
} 

希望它有帮助!