TableViewcontroller.xib created by me should be initial controller - swift

I have created TableViewController by xib not on storyboard. I have view controller which loads by default. Now I want that TableViewController as root controller. Thanks in advance.

1 first register you xib in particular viewController in which you want to load.
by register.nibLod
2 then in AppDelegate in - didFinish method make your that particular viewController as rootviewcontroller.

You can do like this.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let navigationController: UINavigationController = UINavigationController(rootViewController: YourViewController()) //where YourViewController is name of viewController which you want to use as rootViewController
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}

Related

Instantiate initial UIViewController in AppDelegate

I know, there are a lot of similar threads but I'm still struggling to get this problem solved.
I want to instantiate a certain viewcontroller in AppDelegate (only if a user is not logged in). But even without an authentication check my code in app delegate to instantiate my login controller seems to be ignored:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let loginController = UIStoryboard(name: "Main", bundle: .main).instantiateViewController(withIdentifier: "loginController") as! LoginController
self.window!.rootViewController = loginController
self.window!.makeKeyAndVisible()
return true
}
}
No matter which view controller I instantiate in app delegate, the initial view controller set in storyboard appears when launching my app. (Note: if no view controller is set as initial in storyboard, the app is launching with a black screen).
What's my mistake?
Thanks a lot in advance
Alex
You can try this code in didFinishLaunchingWithOptions method --
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialVC: ViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let navigationController = UINavigationController(rootViewController: initialVC)
navigationController.isNavigationBarHidden = true
self.window?.rootViewController = navigationController self.window?.makeKeyAndVisible()
In a modern iOS app, the app delegate has no window. You can create one but it will be replaced. The visible window belongs to the scene delegate. You need to create the interface using scene delegate code.

Why during first boot my navigation bar is under status bar

Why when I create navigation controller with empty view controller I receive strange behaviour with navigation bar?
This is how I create navigation controller.
init(
window: UIWindow,
keystore: Keystore,
navigationController: UINavigationController = UINavigationController()
) {
self.navigationController = navigationController
self.keystore = keystore
super.init()
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
Than I simply do :
func start() {
let x = UIViewController()
x.view.backgroundColor = UIColor.red
navigationController.setViewControllers([x], animated: true)
}
And I receive:
But in the next app launches all is fine.
This might be the case, because you set the NavigationController after the app has already started.
I would recommend to set the NavigationController in your AppDelegate.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let firstViewController = UIViewController()
let navigationController = UINavigationController(rootViewController: firstViewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
}

NavigationViewController stack

I am concerned about NavigationViewController stack in the sense that apple documentation says that "A navigation controller object manages the currently displayed screens using the navigation stack, which is represented by an array of view controllers." And what about another NavigationController attached to the topViewController? Is it in navigation stack? Or there are two navigation stacks? enter image description here and so at the end what viewController will the top of this construction if we will call
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
guard let navController = window?.rootViewController as?
UINavigationController,
let viewController = navController.topViewController else {
return true
}

how to create very first view for Swift 3

Hi I have made a ViewController.
But I don't know how to add very first view right after an app is tapped.
I added below codes in ViewController.swift
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "FirstViewController")
self.present(controller, animated: true, completion: nil)
the FirstViewController appears after the app tapped and shows white background for about a second.
When you first launch your app, the LaunchScreen.storyboard is displayed, as a transition while your app is loading. You can customise this in Interface Builder.
check the option 'is initial view Controller' for that firstViewController from the storyboard.
You can set initial view controller programmatically or via storyboard
Method 1:programmatically--
Set storyboard-identifire for Viewcontroller in Main.storyboard file
like
After that set rootviewController in Appdelegate class.
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
window?.rootViewController = redViewController
window?.makeKeyAndVisible()
return true
}
}
For setting up with navigation controller use UINavigationController -
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryBoard.instantiateViewController(withIdentifier: "viewController") as! viewController
let navigationController = UINavigationController(rootViewController: viewController)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
Method 2:Via storyboard--
Go to respective storyboard & select Is Initial View Controller

How can I open a specific Tab View from AppDelegate

My root view is a tab bar controller, I would like to open the app on a specific tab when a certain notification is received. If I use presentViewController the tab bar disappears. Is there a specific way to do this?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let myTabBar = self.window.rootViewController as! UITabBarController // Getting Tab Bar
myTabBar.selectedIndex = 2 //Selecting tab here
return true
}