Search code examples
iosswiftcallbackuitabbarcontrollerviewdidappear

Call Back in UITabBarController


I have a UITabBarController and a class named it ScreenLocker, in UITabBarController/ViewDidApear I initialize ScreenLocker with a call back, screen locker after c second or when app going to background should call this call back,

class TabBarViewController: UITabBarController {

override func viewDidLoad() {

}

override func viewDidAppear(_ animated: Bool) {
    lockAppCallBack()
}

func lockAppCallBack() {
    let callBack = {
        let myModalViewController = R.storyboard.authentication.authenticationViewController()
        ScreenLocker.isAutoLocked = true
        myModalViewController!.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        myModalViewController!.modalTransitionStyle = UIModalTransitionStyle.coverVertical
        self.present(myModalViewController!, animated: true, completion: nil)
    }
    AuthenticationPatternPresenter.initScreenLocker(callBack: callBack)
}

ScreenLocker

class ScreenLocker: NSObject{
static var isAutoLocked = false
static let instance : ScreenLocker = {

    let instance = ScreenLocker()
    return instance
}()

var timer:Timer!
var time:Int!
var callBack = {

}
func _init(time:Int, callBack: @escaping () -> ()){

    self.time = time
    self.callBack = callBack
}

Each tab has a UINavigationController Everything is ok, but when I change UITabBarController's tab and navigate to next view and back to previous view, tab's ViewDidAppear doesn't call. When I removed Tab bar's ViewDidAppear every thing was ok, When I add again an empty ViewDidAppear in tab bar there was a same problem. Is there any problem with UITabBarController's ViewDidAppear? any suggestion?


Solution

  • You can use ViewWillAppear method for that.

    For more info about app life cycle you can read HERE.

    And HERE is the apple doc.