1

I have a functionality throughout my application. Now i need to call a specific function inside one view controller for some particular condition. So i tried to call, but it fails

main.swift

CommandLine.unsafeArgv.withMemoryRebound(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc))
{   argv in
    _ = UIApplicationMain(CommandLine.argc, argv, NSStringFromClass(MyClass.self), NSStringFromClass(AppDelegate.self))
}

MyClass.swift

class MyClass: UIApplication {
  func resetIdleTimer() {

  }
}

MyViewController.swift

class MyViewController: UIViewController {
    MyClass.resetIdleTimer(); //this causes error in 
}

I need to call resetIdleTimer() inside MyViewController?

gudMorn
  • 13
  • 2
  • Your function is instance type and you are calling it as like class function. – sDev Feb 06 '20 at 13:32
  • i was about to use it as `static func resetIdleTimer()` but that made to change so many place and affects `main.swift`. So another solution? – gudMorn Feb 06 '20 at 14:31
  • you can check my answer, I have checked and it's working. – sDev Feb 06 '20 at 14:58

1 Answers1

0

Check this code, You can call it from anywhere.

if let obj = MyClass.shared as? MyClass {
    obj.resetIdleTimer()
}

For ex. You can call in view controller

class MyViewController: UIViewController {
    override func viewDidLoad() {
      super.viewDidLoad()
      if let obj = MyClass.shared as? MyClass {
          obj.resetIdleTimer()
      }
    }
}
sDev
  • 1,463
  • 9
  • 17