Opening Viewcontroller in Appdelegate While Keeping Tabbar

Opening ViewController In AppDelegate While Keeping Tabbar

First of all, you'll to insatiate a TabBarController:

let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController

And then insatiate all of the viewControllers of TabBarController. If your viewControllers is embedded in to the UINavigationController? If so, you'll to insatiate a Navigation Controller instead:

let first = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let second = storyboard.instantiateViewiController(withIdentifier: "YourSecondNavigationController") as! UINavigationController
let third = storyboard.instantiateViewiController(withIdentifier: "YourThirdNavigationController") as! UINavigationController

Also you should instantiate your desired ViewController too:

let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController

Make all of the NavigationControllers as viewControllers of TabBarController:

tabBarController.viewControllers = [first, second, third]

And check: It's about your choice.

if tabBarController.selectedViewController == first {

// Option 1: If you want to present
first.present(desiredVC, animated: true, completion: nil)

// Option 2: If you want to push
first.pushViewController(desiredVC, animated. true)

}

Make tabBarController as a rootViewController:

self.window = UIWindow.init(frame: UIScreen.main.bounds)   
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()

Finally: It's your completed code:

func openViewController() {

let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController

let first = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let second = storyboard.instantiateViewiController(withIdentifier: "YourSecondNavigationController") as! UINavigationController
let third = storyboard.instantiateViewiController(withIdentifier: "YourThirdNavigationController") as! UINavigationController

let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController

tabBarController.viewControllers = [first, second, third]

if tabBarController.selectedViewController == first {

// Option 1: If you want to present
first.present(desiredVC, animated: true, completion: nil)

// Option 2: If you want to push
first.pushViewController(desiredVC, animated. true)

}

self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()

}

If you want to present or push ViewController when the notification is tapped? Try something like that:

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
openViewController()
completionHandler()

default:
break;
}
}
}

How can I set rootview of a viewcontroller when I have set Tabbar in appdelegate swift?

 let appDelegate = UIApplication.shared.delegate as! AppDelegate

let vc1 = ConstDetailViewController()
//vc1.view.backgroundColor = UIColor.orange
vc1.tabBarItem.title = "Search"
vc1.tabBarItem.image = UIImage(named: "Search")

let vc2 = OptionsViewController()

vc2.tabBarItem.title = "Search"
vc2.tabBarItem.image = UIImage(named: "Street View")

// Set up the second View Controller

//vc2.view.backgroundColor = UIColor.purple

// Set up the Tab Bar Controller to have two tabs
let tabBarController = UITabBarController()

tabBarController.viewControllers = [vc1,vc2]
appDelegate.window?.rootViewController = tabBarController
appDelegate.window?.makeKeyAndVisible()

How to call viewcontroller method from AppDelegate if I use TabBar Navigation

I suggest you using NSNotificationCenter, just post notification inside didReceiveRemoteNotification like this:

NSNotificationCenter.defaultCenter().postNotificationName("refreshNotification", object: nil)

In your HomeViewController in viewDidLoad() you should subscribe to the notification center like this:

NSNotificationCenter.defaultCenter().addObserver( self, selector: "refresh", name: "refreshNotification", object: nil)

Load Tab Bar Controller from AppDelegate

set storyboard ID here

Sample Image
and embed the firstViewController in IB in navigationController

 let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(viewController, animated: true);

Present viewcontroller with tabbar

The solution is to embed every VC in navigationController and then add to TabBarController.

let vc1 = ViewController1()

let navController1 = UINavigationController(rootViewController: vc1)
navController.isNavigationBarHidden = true

let controllers = [navController1, navController2, navController3, navController4]
tabBarController.viewControllers = controllers

window?.rootViewController = tabBarController

Then call

self.navigationController?.pushViewController(controller, animated:
true)

To diplay VC with tabbar

set initial viewcontroller in appdelegate - swift

Xcode11 and SceneDelegate note:

Starting from Xcode11, because of SceneDelegates, it's likely that you shouldn't do it inside AppDelegate. Instead do it from SceneDelegate. For more on that see this other answer



Old answer:

I used this thread to help me convert the objective C to swift, and its working perfectly.

Instantiate and Present a viewController in Swift

Swift 2 code:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let initialViewController = storyboard.instantiateViewControllerWithIdentifier("LoginSignupVC")

self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()

return true
}

Swift 3 code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")

self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()

return true
}

Opening view controller from app delegate using swift

You have to set ViewController StoryBoardId property as below image.

Sample Image

open viewController using coding as below in swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Circles") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()

return true
}

For iOS 13+ (based on an article by dev2qa)

Open SceneDelegate.swift and add following

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

// If this scene's self.window is nil then set a new UIWindow object to it.
self.window = self.window ?? UIWindow()

// Set this scene's window's background color.
self.window!.backgroundColor = UIColor.red

// Create a ViewController object and set it as the scene's window's root view controller.
self.window!.rootViewController = ViewController()

// Make this scene's window be visible.
self.window!.makeKeyAndVisible()

guard scene is UIWindowScene else { return }
}

There is an open-source navigation utility which attempts to make this easier. Example



Related Topics



Leave a reply



Submit