0

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.

4
  • 2
    You can use the 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. Commented Aug 11, 2021 at 2:22
  • 1
    Is there specific reason to have init(coder aDecoder: NSCoder) ? Can you please provide more context how you will use MyTabBarCtrl ? Commented Aug 16, 2021 at 4:43
  • 1
    Possible issues: You don't want to instantiate another one, you want to access it (one that has already been instantiated) from elsewhere. Else, since there are IBoutlet, I guess you have either a Xib or a Storyboard, then look how to init a VC from a Xib or Storyboard... Commented Aug 16, 2021 at 10:14
  • This is a matter of referencing an instance of MyTabBarCtrl from "another class". Without more context about the "another class" and how it relates to MyTabBarCtrl it 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 desired MyTabBarCtrl instance, 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). Commented Aug 16, 2021 at 12:43

2 Answers 2

0
+50

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()
Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what I was looking for. Wish someone could have answered this when I originally posted it. Thanks though
@piddler I guess that's because you've asked an unclear question so I had to guess what do you need, and most of the people did not begin to guess. If you've asked something like "How to call custom tab bar function from a container view controller", you would've get your answer instantly.
okay, that makes sense. Its not always easy to express words.
0

Just delete the required initializer. You're not decoding anything. You're not inheriting from anything that requires that initializer and all your properties are set without a declared initializer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.