gpt4 book ai didi

swift - iOS/swift : How to add one extension to multiple controllers?

转载 作者:可可西里 更新时间:2023-11-01 00:55:37 24 4
gpt4 key购买 nike

在一个名为 Extensions 的文件中,我尝试向 2 个 View Controller 添加一个扩展,这样我就不必将整个代码写两次。

import UIKit
extension TempConvertViewController {
// code
}

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

如有任何帮助,我们将不胜感激!

编辑:谢谢大家的回应。

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

如果我将代码放在一个函数中,keyboardDidShow()keyboardWillHide() 函数将不会在 Controller 本身中被识别。我收到错误消息:使用未解析的标识符“keyboardDidShow(notification:)”,使用未解析的标识符“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(),我不能像这样为另一个 View Controller (LocationChangeViewController) 使用新的扩展:

extension LocationChangeViewController: UITextFieldDelegate {

// calling the function
func keyboardViewManagement()

}

再次感谢大家!

最佳答案

利用协议(protocol)的力量,毕竟我们很快

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
}
}

关于swift - iOS/swift : How to add one extension to multiple controllers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49245933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com