App.Delegate from ViewController to TabController Swift 5 - ios

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

Related

iOS - Calling SceneDelegate method from ViewController

I tried following code to call Scene Delegate func from a VC and getting error
Thread 1: signal SIGABRT
let sceneDelegate = UIApplication.shared.delegate as! SceneDelegate
sceneDelegate.initializeFirstViewController()
Is there any way i can call SceneDelegate func from VC?
UIApplication.shared.delegate is AppDelegate not SceneDelegate You need
if let scene = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate {
// to do
}

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
}
}

iOS access main window

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.

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?

manageObjectContext failure

Simple app starts with a table view in a navigation controller with an add button.
My problem is here:
func saveName(name: String) {
//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
I have issue:
'AppDelegate' does not have a member named 'managedObjectContext'
Thanks.

Resources