iOS / Swift:如何将一个扩展添加到多个控制器?

时间:2018-03-12 23:25:09

标签: swift

在名为Extensions的文件中,我尝试向2个View Controller添加一个扩展程序,因此我不必编写两次整个代码。

import UIKit
extension TempConvertViewController {
// code
}

我需要为LocationViewController使用完全相同的代码。

任何帮助将不胜感激!

编辑: 谢谢大家的回复。

我想要实现的是在2个控制器中重用相同的键盘/视图代码,因为它们都包含textFields。我扩展TempConvertViewController的原因是因为我有一个变量(activeTextField),如果我要在UIViewController中实现它,它将无法识别。

如果我将代码放在函数中,则keyboardDidShow()keyboardWillHide()函数不会在控制器本身中被识别。我收到错误:使用未解析的标识符&keyboardDidShow(通知:),使用未解析的标识符' keyboardWillHide(notification :)' 请参阅下面代码的第二部分

以下是TempConvertViewController

的扩展名
extension TempConvertViewController: UITextFieldDelegate {

    @objc func keyboardDidShow(notification: Notification) {
        let notificationInfo: NSDictionary = notification.userInfo! as NSDictionary
        let keyboardSize = (notificationInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
        let yKeyboard = view.frame.size.height - keyboardSize.height
        let yTextFieldEditing: CGFloat! = activeTextField?.frame.origin.y

        if view.frame.origin.y >= 0 {

            //checking if textField is covered by keyboard
            if yTextFieldEditing > yKeyboard - 60 {
                UIView.animate(withDuration: 0.25, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations:{self.view.frame = CGRect(x: 0, y: self.view.frame.origin.y -
                    (yTextFieldEditing! - (yKeyboard - 60)), width: self.view.bounds.width, height: self.view.bounds.height)}, completion: nil)
            }
        }
    }

    @objc func keyboardWillHide(notification: Notification) {
        UIView.animate(withDuration: 0.25, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: { self.view.frame = CGRect(x: 0, y:0, width: self.view.bounds.width, height: self.view.bounds.height)}, completion: nil)
    }
    func textFieldDidBeginEditing(_ textField: UITextField) {
        activeTextField = textField
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
}

TempConvertViewController

override func viewDidLoad() {
        super.viewDidLoad()

        // Subscribe to Notifications
        let center: NotificationCenter = NotificationCenter.default;
        center.addObserver(self, selector: #selector(keyboardDidShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        center.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

如果我设法将所有内容都包含在一个功能中,例如keyboardViewManagement(),我无法为其他视图控制器(LocationChangeViewController)使用新的扩展程序,如下所示:

extension LocationChangeViewController: UITextFieldDelegate {

// calling the function
func keyboardViewManagement()

}

再次感谢大家!

2 个答案:

答案 0 :(得分:3)

使用协议的力量,毕竟我们是快速的

protocol Extendable: class {
    var editingTextField: UITextField? { get }
    func someFunc()
}

extension Extendable where Self: UIViewController {
    func someFunc() {
        // yor implementation
    }
}

class ViewController: UIViewController {
    var editingTextField: UITextField? {
        return activeTextField
    }
}

答案 1 :(得分:0)

只需写下UIViewController的扩展名:

extension UIViewController {
    //anything can go here

    func some() {
    }
}

然后在你的视图控制器中:

class VC1: UIViewController {
    some()
}