AppDelegate not showing Storyboard buttons - swift

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]

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.

Call navigationController from AppDelegate

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

'Auto-login' not working anymore

I am using a firebase project for my app, on which users have to create an account to access to the content of the app.
In order to avoid users having to log in each time they launch the app, I put (in the AppDelegate) :
if Auth.auth().currentUser != nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "tabBar")
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
} else {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "InscriptionViewController")
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
}
And this piece of code did work for the past months but now it is not (all my pods are up to date)
Why is it not working anymore ?
Your AppDelegate should looks like this.
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
if Auth.auth().currentUser != nil {
//your code
}
}
}
Try to download google-info.plist

Self.revealViewController() returns nil

I am currently trying to get the pan gesture to reveal the controller. As of right now, my open button works, but when I add the last line, my program crashes because revealViewController is nil. Link to pod: https://github.com/John-Lluch/SWRevealViewController.
import Foundation
import UIKit
class MainViewController : UIViewController{
#IBOutlet weak var mainButton: UIView!
#IBOutlet weak var Open: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated);
mainButton.makeCircle();
Open.target = self.revealViewController()
Open.action = #selector(SWRevealViewController.revealToggle(_:));
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer());
}
}
It's likely you haven't set your SWRevealViewController to be the rootViewController. Remember to set Custom Class and Storyboard ID to be SWRevealViewController in your Identity inspector.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
self.view.window?.rootViewController = rootVC
// Cont. with your code...
}
In case you need to show a login page at the beginning, you need a function in you AppDelegate, which allows the window to update the rootViewController
func changeRootVCToSWRevealVC () {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
if let window = self.window{
window.rootViewController = rootVC
}
}
After user logged in, you can change the rooViewController to SWRevealViewController.
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.changeRootVCToSWRevealVC()
}
If you have a variable storing the state of the last log-in, and want to navigate directly to the Navigation Drawer menu rather than LoginViewController.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let rootVCId = isLoggedin ? "SWRevealViewController" : "LoginViewController"
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewController(withIdentifier: rootVCId)
self.view.window?.rootViewController = rootVC
// Cont. with your code...
}
I was just having this same issue and came across this post. My issue turned out to be more of an "oops!" as I missed something obvious and spent 30 minutes scratching my head on it. :facepalm:
If the accepted answer doesn't work for you, check to make sure you connected your menu property to the storyboard with an outlet.
Swift:
#IBOutlet weak var menuButton:UIBarButtonItem!
override func viewDidLoad() {
if (self.revealViewController() != nil) {
menuButton.target = self.revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
}
}

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