Is it possible to define a “general” unwind mode in a storyboard?

I am new to iOS development and am leaving Android background. I am looking for a way to return to the original view controller after the user logs out.

This is a post. How can I return to the initial view controller in Swift? recommends using unwinding.

However, since the user session for my application can expire at any time (and during any view controller). That would mean that I would need to rewind the transition from each view controller to the root view controller! In addition, this will also lead to some duplicate code, since each view controller will need a way to reference its own unique segue identifier.

Initially, I was hoping for a subclass UIViewControllerand did CustomViewController, after which all my other ProfileViewController, LoginViewControllersubclasses will be.

CustomViewControllerwill provide helper methods such as func logOut(). Is there a way to do this, perhaps through some sort of general segue reversal?

+4
source share
2 answers

. ViewController, segue, ViewController. .

BaseViewController, View BaseViewController. , , BaseViewController ( , ).

,

class BaseViewController : UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //implement your logic to monitor if user is still logged in if user not logged in simply call
        self.logOut()
    }

    func logOut() {
       //each child view controller will override and handle log out condition specifically for that ViewController
    }
}

, ViewController, , viewController . , initialViewController - rootViewController , BaseViewController

   override func viewDidLoad() {
        super.viewDidLoad()
        //implement your logic to monitor if user is still logged in if user not logged in simply call
        self.logOut()
        UIApplication.shared.keyWindow?.rootViewController?.navigationController?.popToRootViewController(animated: true)
    }

:

, , ViewController, ViewController .

,

  • ViewController , , , , , .

  • , View, , ?

  • VC ViewController, , viewController, , , , , ?? ?

?

.

AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
   //check if user is logged in

   guard let window = UIApplication.shared.keyWindow else {
        return
   }

   if (userIsLoggedIn) {
      //load view controller that you wanna show after login lets say Home
      let homeVC = //instantiate view controller from story board
      UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
            window.rootViewController = homeVC
        }, completion: { completed in
            // maybe do something here
        })
   }
   else {
       //load initial controller 
      let initialVC = //instantiate view controller from story board
      UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
            window.rootViewController = initialVC
        }, completion: { completed in
            // maybe do something here
        })
   }
}

, VC VC, , , root VC

      let initialVC = //instantiate view controller from story board
      UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
            window.rootViewController = initialVC
        }, completion: { completed in
            // maybe do something here
        })
+3

, () . , . " " .

+1

Source: https://habr.com/ru/post/1693908/


All Articles