【问题标题】:Making code more organised in swift IOS在 swift IOS 中使代码更有条理
【发布时间】:2016-10-31 00:38:10
【问题描述】:

我正在努力使我的代码更有条理和可重用。我有一些功能和通知,允许在键盘显示时向上移动滚动视图并在键盘隐藏时向下滚动。这一切都在运作。但是,我会想象这些功能将用于我的项目的多个部分,这些部分在 UIViewcontroller 中有滚动视图。所以我想创建一个更可重复使用的代码,而不是在多个视图控制器中编写相同的代码。

目前,在我的一个视图控制器中,我有

var keyboard = CGRect() 

override func viewDidLoad() {

    //  Check notifications of keyboard activity
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PostVC.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(PostVC.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)

    // Tap to hide keyboard
    let hideTap = UITapGestureRecognizer(target: self, action: #selector(PostVC.hideKeyboard))
    hideTap.numberOfTapsRequired = 1
    self.view.userInteractionEnabled = true
    self.view.addGestureRecognizer(hideTap)
}

func hideKeyboard() {
        self.view.endEditing(true)
    }


func keyboardWillShow(notification: NSNotification) {

    keyboard = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey]!.CGRectValue())!

    UIView.animateWithDuration(0.4) {
        self.scrollView.contentSize.height = self.view.frame.size.height + self.keyboard.height / 2 + UITabBarController().tabBar.frame.size.height
    }
}

func keyboardWillHide(notification: NSNotification) {

    UIView.animateWithDuration(0.4) {
        self.scrollView.contentSize.height = 0
    }
}

我对尝试使代码更可重用有点陌生。我不确定是否需要创建一个新类或只是创建 UIViewcontroller 的扩展并将其放入其中。我尝试创建 UIViewcontroller 的扩展并执行类似的操作

func keyboardWillShow(notification: NSNotification, _scrollView: UIScrollView) { }

并将滚动视图的实例 (@IBOutlet weak var scrollView: UIScrollView!) 传递给函数。但是,我在做#selector(keyboardWillShow(_:, keyboard: keyboard, scrollView: scrollView) 时遇到了麻烦。它给了我一个错误,说表达式列表中的预期表达式(我认为它在抱怨_:)。我可能走在一条完全错误的道路上。任何人都可以请帮助。

谢谢,

【问题讨论】:

  • 如果代码可以在应用程序的许多地方重用,最好为共享代码的所有视图控制器创建一个基类。
  • 您是否考虑过在 UIScrollView 之类的东西上创建 class extensions

标签: ios iphone xcode swift keyboard


【解决方案1】:

我建议创建一个协议并将默认实现添加为协议扩展。您可以在协议中添加实现它的类应该有一个滚动视图和一个键盘。请记住,协议扩展比基类更灵活,这就是为什么在 Swift 中经常首选的原因。

这是一个例子

protocol Scrollable : class {

    var scrollView: UIScrollView { get }
    var keyboardRect: CGRect { get set }
}

extension Scrollable where Self : UIViewController {

    func registerForKeyboardNotifications() {

        NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillShowNotification, object: nil, queue: nil, usingBlock: { (notification) in

            self.keyboardWillShow(notification)
        })
        NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillHideNotification, object: nil, queue: nil, usingBlock: { (notification) in

            self.keyboardWillHide(notification)
        })
    }

    func deregisterForKeyboardNotification() {

        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func keyboardWillShow(notification: NSNotification) {

        self.keyboardRect = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey]!.CGRectValue())!

        UIView.animateWithDuration(0.4) {
            self.scrollView.contentSize.height = self.view.frame.size.height + self.keyboardRect.height / 2 + UITabBarController().tabBar.frame.size.height
        }
    }

    func keyboardWillHide(notification: NSNotification) {

        UIView.animateWithDuration(0.4) {
            self.scrollView.contentSize.height = 0
        }
    }
}

请记住,我使用了 addObserverForName 而不是 addObserver,因为后者不能轻松地与协议扩展一起使用。更多相关信息,您可以在此处阅读 - Swift make protocol extension a Notification observer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多