iOS access main window - ios

How to access main window try this one but it is not working for me :
if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
window.addSubview(offerView)
}
also i try with this way but no luck for me :
if let window = UIApplication.shared.windows.first {
window.addSubview(offerView)
}
Note: keyWindow work for me but Using keyWindow can return a keyboard or UIAlertView window, which lie above my application window.
Here is existing question i found but that answer not working for me
How do I get the keyWindow reference in a swift app?
on my application one offer view appear from bottom of the page. that offer will be shown half of the screen

let appDelegate = UIApplication.shared.delegate as? AppDelegate
let rootViewController = appDelegate?.window?.rootViewController
You can also cast the rootViewController if its a custom class i.e.
let rootViewController = appDelegate?.window?.rootViewController as? YourCustomViewController

The easiest solution will be to add a property variable window of type UIWindow in AppDelegate and adding rootviewcontroller to that window property and accessing that window property wherever you need.

Related

App.Delegate from ViewController to TabController Swift 5

My code does not work in Swift 5
var myTabBar = self.storyboard?.instantiateViewController(withIdentifier: "myTabBar") as! UITabBarController
var app = UIApplication.shared.delegate as! AppDelegate
app.window?.rootViewController = myTabBar
Value of type AppDelegate has no member window
Because in new Xcode window is moved to SceneDelegate
I'm meeting you more than half-way here, because we really need more details on what's going on and what you're trying to achieve.
But the error is telling you that the AppDelegate doesn't have a variable called window. You probably want the UIApplication object itself, and not its delegate.
Maybe try replacing
var app = UIApplication.shared.delegate as! AppDelegate
app.window?.rootViewController = myTabBar
with
var app = UIApplication.shared
app.keyWindow?.rootViewController = myTabBar

popToRootViewController from NSObject Class

I have been trying to find the solution but none of them available are working for me, Can someone please advise how can I achieve this?
I have set a rootviewcontroller as a view controller(Container Viewcontroller) by using the following code after successful login.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ContainerViewController") as! ContainerViewController
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = vc
Based on a specific notification received, I have defined a custom function which is being called for sure but I want my app user to go back to root automatically regardless of the navigation controller he is currently on. I am using this code
guard let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController else { return }
navigationController.popToRootViewController(animated: true)
Flow is Root(VC)->NavigationController1->PushingViewController2->PushingViewController3, suppose user is currently at ViewController3(which is pushed) and I want to take the user back from anotherClass NSOBject which is observing network Status and upon network failure it is calling ABC function but when I add the above navigation controller reference to pop all navigationcontrollers it is not working.

How to fix "Unexpectedly found nil" in app Delegate when setting UITabBarController as root view controller

enter image description hereAs per the screenshots, I was trying to set a UITabBarController as my root view controller. In the storyboard, a UITabBarController is my initial VC, however I get this issue.
I check the storyboard if the tab bar controller is my initial view controller, and if I messed up with unwrapping anything.
let tabController = window!.rootViewController as! UITabBarController
if let tabViewControllers = tabController.viewControllers {
let navController = tabViewControllers[0] as! UINavigationController
let controller = navController.viewControllers.first as! CurrentLocationViewController
controller.managedObjectContext = managedObjectContext
}
return true
}
expected: To run smoothly
actually result: the app launches, crashes on the launch. and I get the following error:
THREAD ONE FATAL ERROR: Unexpectedly found nil while unwrapping optional.
That line is crashing because either window is nil or the view controller is not a UITabBarController. If you remove the code inside didFinishLaunchingWithOptions does your app load without crashing and display the view controller you are expecting?
If so, maybe try moving the code to set the managed object context inside CurrentLocationViewController's viewDidLoad method:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
managedObjectContext = appDelegate.managedObjectContext
If the app still crashes or does not display the tabbed view controller you are expecting, you might not have the initial view controller set up correctly.
Its an ugly way of doing it though...
to be super safe do this:
guard let tabController = window!.rootViewController as? UITabBarController else { return }
let viewControllers = tabControllers.viewControllers
if let navController = viewControllers.first as? UINavigationController {
if let controller = navController.viewControllers.first as? CurrentLocationViewController {
controller.managedObjectContext = managedObjectContext
}
}

How to pass data to nth view controller on the UINavigationController stack?

There are 2 apps A & B.
App A has a url to open App B.
Soon as App B opens, it has to load 5 view controllers onto the navigation stack which is done by the following code:
let LandingVC = self.storyboard?.instantiateViewControllerWithIdentifier("LandingVC") as! LandingVC
let Dashboard = self.storyboard?.instantiateViewControllerWithIdentifier("Dashboard") as! Dashboard
let PlayerVC = self.storyboard?.instantiateViewControllerWithIdentifier("PlayerVC") as! PlayerVC
let PlayerDetailVC = self.storyboard?.instantiateViewControllerWithIdentifier("PlayerDetailVC") as! PlayerDetailVC
let ScoreReportVC = self.storyboard?.instantiateViewControllerWithIdentifier("ScoreReportVC") as! ScoreReportVC
let viewControllersList = [LandingVC, Dashboard, PlayerVC, PlayerDetailVC, ScoreReportVC]
self.navigationController?.setViewControllers(viewControllersList, animated: false)
From ScoreReportVC I need to be able to set variables on previous ViewControllers so that the user can navigate to previous screens even though they fired the application from another application.
Here is what I have tried: defined a protocol in previous view controllers that are behind ScoreReportVC that are on the stack and inside ScoreReportVC like so:
for viewcontroller in self.navigationController?.viewControllers {
if viewcontroller is PlayerDetailVC {
PlayerDetailVC.delegate = self
}
if viewcontroller is PlayerVC {
PlayerVC = self
}
if viewcontroller is Dashboard {
Dashboard.delegate = self
}
if viewcontroller is LandingVC {
LandingVC.delegate = self
}
}
But the delegates are not getting called. Any help how to correctly pass data to all the ViewControllers on the stack would be greatly appreciated.
For relatable classes we use Delegations. But the classes which are not relatable , for those, we use Notifications. In your case Notifications will be needed for implementation and to pass data from one VC to another VC.
A better approach would be to create/set the variables when the controllers are created.

How to use a variable in other class that is defined in AppDelegate

I have a variable var window: UIWindow? in AppDelegate.swift file inside class AppDelegate that I want to use in other class xyz.swift file inside class xyz as explained in here Get current view controller from the app delegate (modal is possible) but I am getting error at the first line any help will be appreciated. here is the code from xyz.swift
func CurrentView() -> UIView
{
let navigationController = window?.rootViewController as? UINavigationController // Use of Unresolved identifier 'window'
if let activeController = navigationController!.visibleViewController {
if activeController.isKindOfClass( MyViewController ) {
println("I have found my controller!")
}
}
}
Even if I use let navigationController = AppDelegate.window?.rootViewController as? UINavigationController error is 'AppDelegate.Type' does not have member named 'window'
You may have to do as follows:
var appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let navigationController = appDelegate.window?....
As #Dave Durbin has pointed out you are trying to use a variable defined in one class into another class without the reference of the defining class.
This line of code is in xyz.swift
let navigationController = window?.rootViewController as? UINavigationController // Use of Unresolved identifier 'window'
You don't provide any context for window so it's expected to be in this class or a global variable.
This is closer:
navigationController = AppDelegate.window?.rootViewController as? UINavigationController
and you seem to realise that you need to reference the window variable within your AppDelegate instance however the syntax you are using references a static variable and window is a member variable.
I suggest you read through the swift manual and gain a better understanding of variable scopes and check this:
How do I get a reference to the app delegate in Swift?

Resources