使用所需的初始化编码器从另一个 class 调用方法

call a method from another class with required init coder

我有一个 class 和一个必需的初始化编码器,我有一个方法想从另一个 class 调用。我不确定如何实例化这个 class.

先class我要打电话

import UIKit
import SwiftIcons

//weak var tabInstance = MyTabBarCtrl(coder: NSCoder.init())  //what I tried


class MyTabBarCtrl:  UITabBarController, UITabBarControllerDelegate {

@IBOutlet weak var tabbar: UITabBar!
let button = UIButton.init(type: .custom)

public var floatingButton: UIButton?
private let floatingButtonImageName = ""
private static let buttonHeight: CGFloat = 60.0
private static let buttonWidth: CGFloat = 60.0
private let roundValue = MyTabBarCtrl.buttonHeight/2
private let trailingValue: CGFloat = 157.5
private let leadingValue: CGFloat = 17.0
private let shadowRadius: CGFloat = 2.0
private let shadowOpacity: Float = 0.5
private let shadowOffset = CGSize(width: 0.0, height: 5.0)
private let scaleKeyPath = "scale"
private let animationKeyPath = "transform.scale"
private let animationDuration: CFTimeInterval = 0.4
private let animateFromValue: CGFloat = 1.00
private let animateToValue: CGFloat = 1.05
var tabSelected:String = ""
fileprivate lazy var defaultTabBarHeight = { tabBar.frame.size.height }()

required init(coder aDecoder: NSCoder) {
 super.init(coder: aDecoder)!
}

override func viewDidLoad() {
 super.viewDidLoad()
 self.delegate = self
 floatingButton = UIButton(type: .custom)

}

public override func viewWillAppear(_ animated: Bool) {
       super.viewWillAppear(animated)
       createFloatingButton()
    //tabbar.tabBarItem.imageInsets = UIEdgeInsets.init(top: 5,left: 0,bottom: -5,right: 0)
   }

  override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    let newTabBarHeight = defaultTabBarHeight + 16.0

    var newFrame = tabBar.frame
    newFrame.size.height = newTabBarHeight
    newFrame.origin.y = view.frame.size.height - newTabBarHeight

    tabBar.frame = newFrame 
    }

    public override func viewWillDisappear(_ animated: Bool) {
       guard floatingButton?.superview != nil else {  return }
       DispatchQueue.main.async {
           self.floatingButton?.removeFromSuperview()
           self.floatingButton = nil
       }
       super.viewWillDisappear(animated)
   }


  public func changeFloatingButtonColor(){
    floatingButton?.backgroundColor = Theme.colorGoodMorningToolbar
  }

我想从第二个控制器调用 changeFloatingButtonColor() 它抱怨 nscoder 但我无法将其从第一个控制器中删除。

只需删除所需的初始值设定项。你没有解码任何东西。您没有继承任何需要该初始化程序的东西,并且您的所有属性都是在没有声明的初始化程序的情况下设置的。

您似乎正在尝试从其中一个选项卡更新选项卡栏控制器。

您可以使用 tabBarController 变量从您的第二个控制器获取对当前选项卡栏控制器(如果有一个显示此特定视图控制器的控制器)的引用,试试这个:

(tabBarController as? MyTabBarCtrl)?.changeFloatingButtonColor()