简体   繁体   中英

How to move from Viewcontroller to TabBarController without segue

I have such logic in my app:

在此处输入图像描述

I would like to move from ViewController to HomeScreen after 200 response from server, and in any other cases it will be done programmatically. I know about segue which can be attached to pressing button, but when I use segue and press login btn without text in fields I also move to Home Screen that is why I think that segue can't be used in such situation (I can be wrong), so I added id for Home Screen and added such code for pressing btn:

let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "homeScr") as? UITabBarController
self.navigationController?.pushViewController(vc!, animated: true)

and when I press this button noting happens. What I did wrong and how to solve this problem?

The controller you push cannot be a Tabbarcontroller according to apple documentation .

The view controller to push onto the stack. This object cannot be a tab bar controller.

Edit: As alternative this could be a solution:

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "mainApp") as! UITabBarController
nextViewController.modalPresentationStyle = .fullScreen
self.present(nextViewController, animated:true, completion:nil)

In your appDelegate, add property:

final var window: UIWindow?

Open main like this in your Login view controller

func openMain() {
    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: 
    "mainApp") as! UITabBarController


    let window = UIApplication.shared.delegate!.window!!
    window.rootViewController = nil
    window.rootViewController = nextViewController
        
    UIView.transition(with: window, duration: 0.4, options: [.transitionCrossDissolve], animations: nil, completion: nil)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM