Currently, I build simple app music and I would like to do if I left my current viewcontroller(musicController) is playing to anotherController and I created one button "NowPlaying" put on anotherController to go my currently viewcontroller(musicController) without load any view. Please help me
Thank you
Try my working code,
var currentTopMostChildCont: UIViewController = self.getCurrentTopMostChildController()
func getCurrentTopMostChildController() -> UIViewController {
var topController: UIViewController = UIApplication.sharedApplication().keyWindow.rootViewController
while topController.childViewControllers.count() > 0 {
topController = topController.childViewControllers.last //You can track your viewController which you want & get it.
}
return topController
}
I hope this will surely help you.
Related
I'm working on an app that displays a today extension with some information. When I tap on the today extension, it opens the app and navigates to a subview from the root to display the information. Normally the user would then click the back arrow to go back to the main view, but there is no way to tell if this is actually done. It is possible for the user to go back to the today extension and tap again. When this is done, the subview is opened once again with new information. If this is done a bunch of times, I end up with a bunch of instances of the subview and I have to click the back button on each of them to get back to the main view.
My question: Is it possible to check if the subview is already visible? I'd like to be able to just send updated information to it, instead of having to display an entirely new view.
I am currently handling this by keeping the instance of the UIViewController at the top of my root. If it is not nil, then I just pass the information to it and redraw. If it is nil, then I call performSegue and create a new one.
I just think that there must be a better way of handling this.
Edit: Thanks to the commenter below, I came up with this code that seems to do what I need.
if let quoteView = self.navigationController?.topViewController as? ShowQuoteVC {
quoteView.updateQuoteInformation(usingQuote: QuoteService.instance.getQuote(byQuoteNumber: quote))
}
else {
performSegue(withIdentifier: "showQuote", sender: quote)
}
This is different from the suggested post where the answer is:
if (self.navigationController.topViewController == self) {
//the view is currently displayed
}
In this case, it didn't work because I when I come in to the app from the Today Extension, it goes to the root view controller. I needed to check whether a subview is being displayed, and self.navigationController.topViewcontroller == self will never work because I am not checking to see if the top view controller is the root view controller. The suggestions in this post are more applicable to what I am trying to accomplish.
u can use this extension to check for currently displayed through the UIApplication UIViewController:
extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return topViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
if let selected = tab.selectedViewController {
return topViewController(base: selected)
}
}
if let presented = base?.presentedViewController {
return topViewController(base: presented)
}
return base
}
}
and usage example:
if let topController = UIApplication.topViewController() {
if !topController.isKind(of: MainViewController.self) { //MainViewController- the controller u wish to equal its type
// do action...
}
}
I have a problem with the above stated. I can not find the exact information on the forums. Most of them are outdated and I have written the code programmatically. I have a controller that contains a view to edit the profile. I can not access that after changing the function listed below. I have a rootviewcontroller set to something else, but I tried the UiApplication calls anyway and it return nil and I can not open the profile controller. This is the function listed below.
#objc func handleOpen2() {
(UIApplication.shared.keyWindow?.rootViewController as? BaseSlidingController)?.openMenu()
}
Xcode does not give me an error but I can not get my menu to open. My rootviewcontroller is set to something else in app delegate. I have a controller that is used to control the sliding menu when I press the edit profile button.
func openMenu() {
isMenuOpened = true
redViewLeadingConstraint.constant = menuWidth
redViewTrailingConstraint.constant = menuWidth
performAnimations()
setNeedsStatusBarAppearanceUpdate()
}
This code is used to open my side bar menu with the information I need and also to perform animations as well. I was wondering if someone had any idea what I can do different instead in my handleOpen2 function. If you need more code, please let me know. Thanks
On swift 5 version:
(UIApplication.shared.windows.first?.rootViewController as? BaseSlidingController)?.openMenu()
And Thanks in advance for any help.
I have a an embedded view that I want to load in multiple ViewControllers. this is working fine. The problem comes when I try to get the background color of the main View.
Image is VC setup
So I have tried all sorts. I thought about using userDefaults and save the color when the main VC loads and get the colour from userDefaults in the embedded VC, but this feels like a bit of a hack.
The Code below obviously will only get the colour of the view in the embedded VC. Im thinking there must be away to check superview or something?
if self.view.backgroundColor!.isEqual(UIColor.White) {
print("Background is White")
} else {
print("not White")
}
I hope this makes sense. Again Thanks in advance for any enlightenment.
You can try this if it's the root
if let roo = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController {
print(roo.view.backgroundColor)
if roo.view.backgroundColor == .white {
print("Yes")
}
else {
print("No")
}
}
if not You can also create a delegate like
class ChildVC: UIViewController {
weak var delegate:ParentVC?
}
and in viewDidLoad of parent
if let fir = self.children.first as? ChildVC {
fir.delegate = self
}
After that you can get the parent color with
delegate?.view.backgroundColor
I have an app written in swift that displays location information on different views. I am receiving location updates in AppDelegate (I don't want to use a singleton class since I want the location updates even when the app is in the background).
Now, I am using SWRevealViewController to implement a sidebar menu to toggle between the different views. When a new location update is received, how do I call the function of the viewController that is currently active to update the UI?
I searched a lot and all the solutions that talk about how to find the current UIViewController actually return SWRevealViewController as the current UIViewController, which doesn't help.
I gave it another shot and was able to find my viewController in my app from another view controller. However, I couldn't get my child view controllers app delegate.
Looking forward to other answers...
here's what I did:
let app = UIApplication.sharedApplication().delegate! as! AppDelegate
if let nav = app.window?.rootViewController?.childViewControllers {
nav.forEach { vc in
if let viewControllers = vc as? UINavigationController {
viewControllers.childViewControllers.forEach { vc in
if let x = vc as? ViewController {
print("Got it \(x)")
}
}
}
}
}
So i have this app and on the first open i want it to show a quick start page. The code i have to initiate and work out if its there first open is
let savedData = NSUserDefaults(suiteName: "xxx")
if savedData?.boolForKey("firstTimeUser") == nil {
savedData?.setObject(true, forKey: "firstTimeUser")
savedData?.synchronize()
print("Hello")
}
if savedData?.boolForKey("firstTimeUser") == true {
//Code to show view controller! Help :)
savedData?.setBool(false, forKey: "firstTimeUser")
savedData?.synchronize()
}
I have a NSViewController on my main.storyboard and i want it to present it self as a sperate window that the user can close after they have finished reading it.
Im thinking i have to use a storyboard id but iv gotten lost and need help!
Thanks in advance!