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.
init(frame:.zero)initialiser, but I don't think this is really what you want to do. Presumably you have an instance of this view controller already on screen somewhere; You need a reference to that instance so that you can call the function.init(coder aDecoder: NSCoder)? Can you please provide more context how you will useMyTabBarCtrl?MyTabBarCtrlfrom "another class". Without more context about the "another class" and how it relates toMyTabBarCtrlit is really hard to suggest something without writing a book, since the problem is very generic. Most likely you'll need some kind of variable in "another class" that points to the desiredMyTabBarCtrlinstance, that you use to call that instance method. Another way (and here goes a very bad advice) is using NotificationCenter (don't use it for that, please).