Call navigationController from AppDelegate - swift4

Now I am implementing onesignal notification, when user click on notification I want to open specific Viewcontroller.
Here is my code at AppDelegate
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let detailBrand = storyBoard.instantiateViewController(withIdentifier: "PagerOverviewControlerID") as! PagerOverviewControler
detailBrand.getValue = value
navigationController?.pushViewController(detailBrand, animated: true)
This code is working fine if I put it in any ViewController Class but inside AppDelegate it doesn't.
Please help!!!

Well, first you have to understand that the Appdelgate is not a UIViewController,
therefore you can't use pushViewController(detailBrand, animated: true) because you are not exactly in a UIVewController to be able to do so, But Instead you can initiate a UINavigatetionController set it as your root then push from it.
and your code should be something like this
let rootViewController = self.window!.rootViewController as! UINavigationController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "PagerOverviewControlerID") as! PagerOverviewControler
rootViewController.pushViewController(profileViewController, animated: true)
Also keep in mind you should place it inside the didFinishLaunchWithOption method.
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool

Related

AppDelegate not showing Storyboard buttons

I am setting up a MainViewController holding multiple ViewControllers the following way:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let pageController = UIPageViewController(
transitionStyle: UIPageViewControllerTransitionStyle.scroll,
navigationOrientation: UIPageViewControllerNavigationOrientation.horizontal,
options: nil
)
let navigationController = MainViewController(rootViewController: pageController)
navigationController.view.backgroundColor = UIColor.white
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let leftView = storyboard.instantiateViewController(withIdentifier: "VC1")
let middleView = storyboard.instantiateViewController(withIdentifier: "VC2")
let rightView = storyboard.instantiateViewController(withIdentifier: "VC3")
navigationController.viewControllerArray = [leftView, middleView, rightView]
self.window!.rootViewController = navigationController
self.window!.makeKeyAndVisible()
return true
}
}
My MainViewController also has what is supposed to be a floating button. I have aded this in the storyboard (see picture below). The problem is that the button is not showing. How do i make it show?
I know that somehow this problem is related to the fact that I adding MainViewController in the AppDelegate, but I am not sure how that is hiding the buttons.
You aren't creating your MainViewController instance from the storyboard, you are just creating it with a call to its initialiser. This means that none of your storyboard elements will be loaded
You need something like:
let navigationController = storyboard.instantiateViewController(withIdentifier: "Main")
navigationController.viewControllers = [pageController]

Navigation through TabbarController And NavigationController. How?

i have storyboard file like this:enter image description here
when user is loggedin i make segue with:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialVC: UITabBarController = storyboard.instantiateViewController(withIdentifier: "loggedin") as! UITabBarController
self.present(initialVC, animated: true, completion: nil)
and automaticly open viewcontroller with identifier "selectsaloon", but sometimes depending on the condition I need to go to viewcontroller with identifier "searchparams". help me find a solution
You can Make navigation on viewdidload for selectsaloon when condition occuer
if (condition) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialVC: UIVieWController = storyboard.instantiateViewController(withIdentifier: "searchparams") as! UIViewController
self.navigationController.push(initialVC, animated: true)
}

Setting Intialview controller embedded in UINavigationController

I have few view controllers embedded in UINavigationController. The first view controller is login page. The second view controller is the home page. I want initialview controller as second view controller when the user is already logged in.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
// Override point for customization after application launch.
if let data = Locksmith.loadDataForUserAccount(userAccount: "someString")
{
if let userAccessToken = data["accessToken"]
{
if (userAccessToken as! String) != ""
{
let initialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
loginResponse = LoginResponse()
loginResponse?.UserAccessToken = userAccessToken as? String
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
}
}
return true
}
The problem is the subsequent view controllers are not embedded in navigation controller. Since it is not embedded in navigation controller I am not able to goback from one view controller to the other.
Add this in App delegate
first check user already login or not, if login then execute this code
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as! ViewController
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = redViewController
Use this in App Delegate and add an extension of UIStoryboard.
func checkForAlreadyLogin() {
let dashBoardScreen = UIStoryboard.dashBoardScreen()
let loginController = UIStoryboard.loginController()
if UserDefaults.standard.bool(forKey: UserDefaultValues().LOGINSTATUS){
self.window!.rootViewController = dashBoardScreen
}else {
self.window!.rootViewController = loginController
}
}
public extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: Bundle.main) }
class func dashBoardScreen() -> HomeViewController?{
return mainStoryboard().instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
}
internal class func loginController() -> LoginViewController?{
return mainStoryboard().instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
}
}
Just call this checkForAlreadyLogin() method in 'didFinishLaunchingWithOptions'.
Also remember to set StoryboardID in the storyboard for each viewController.
This code did the trick for me.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if let data = Locksmith.loadDataForUserAccount(userAccount: "someString")
{
if let userAccessToken = data["accessToken"]
{
if (userAccessToken as! String) != ""
{
let initialViewController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.pushViewController(initialViewController, animated: true)
}
}
}
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

Open a View that is not the root one and keep the tabbarcontroller from the root view (swift)

Hi guys I have an application with a view organization like the picture bellow:
Views:
My Application has to open View B (from the picture) when an user opens an attachment (I have another question Open specific view when user opens an attachment which I have found the answer for this question). I have done that already my problem is that when I open View B the tabbar is not present.
Is there a way that when I open View B I see the tabbar that is part of the root view??
Update:
Here is the code that I use to open the view from the AppDelegate.swift file:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("AddComment") as! AddCommentViewController
window?.rootViewController = vc
}
Just saw your updated post. In order for this to work, the following has to be true:
window?.rootViewController is a UITabBarController
The view controller at indexOfNavigationViewControllerInTabBar in the tab bar is a UINavigationViewController.
If so, then do:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
let indexOfNavigationViewControllerInTabBar = 0 // Set yourself
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("AddComment") as! AddCommentViewController
let tabBarController = window?.rootViewController as! UITabBarController
let navigationController = tabBarController.viewControllers![indexOfNavigationViewControllerInTabBar] as! UINavigationController
navigationController.setViewControllers([vc], animated: true)
tabBarController.selectedIndex = indexOfNavigationViewControllerInTabBar
}