I have a class with a required init coder that I have a method I would like to call from another class. Im unsure how to instantiate this class.
first class I want to call
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
}
I would like to call changeFloatingButtonColor() from a second controller it complains about nscoder but I can't remove it from the first controller.
Looks like you're trying to update tab bar controller from one of tabs.
You can get reference to current tab bar controller (if there's one that's presenting this particular view controller) from your second controller using tabBarController
variable, try this:
(tabBarController as? MyTabBarCtrl)?.changeFloatingButtonColor()