I've got a UITabBarController embedded in a UIViewController. I'm trying to access the UITabBarController instance from the parent UIViewController.
vc.children has a return type of [UIViewController] so the UITabBarController doesn't show up.
vc.tabBarController is nil because the view is not embedded in the TabBarController. My setup is the other way around.
Any ideas?
My storyboard:
Fastest solution:
vc.children.compactMap({$0 as? UITabBarController}).first .
Best solution:
Select the Embed Segue from the storyboard and give an identifier (say "containerEmbedSegue". Next, in your vc:
var tabBarVC: UITabBarController?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "containerEmbedSegue" {
self.tabBarVC = segue.destination as? UITabBarController
}
}
I needed to move window?.rootViewController = viewController before vc.children.
Related
I am trying to do something this:
I want to pass some data to ApplicationsViewController which is embedded in nav bar, therefore I get the error: Could not cast value of type 'UINavigationController'
I am not able to understand how to implement the same. Help is much appreciated. Thank you.
The reason is you can't pass data directly to ApplicationViewController as InboxViewController segue's destination is UINavigationController.
So, first you need to access UINavigationController and after that ApplicationsViewController from the stack.
Try the code below:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToApplications" {
if let navigation = segue.destination as? UINavigationController, let applicationVC = navigation.topViewController as? ApplicationsViewController {
applicationVC.id = "RGB"
}
}
}
when pass data application view controller which is rootviewController of UINavigationController. you should give segue name of navigation controler and type cast as UINavigationController as destination viewcontroller.
Fetch rootviewController which is application View controller and assign value of id
Here is code how to assign value to Application ViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "navigationSegue" { // segue of navigationVC
let navVc = segue.destination as! UINavigationController
let appVc = navVc.viewControllers.first as! ApplicationsViewController
appvc.id = "RGB"
}
}
I have 3 view controllers. First: NavigationController, Second: TableViewController and Third: DetailsTableViewController (connected respectively)
And then I need to add a custom top bar buttton to DetailsTableViewController but I can't. Because there is no NavigationController before the DetailsTableViewController. When I add a NavigationController between TableViewController and DetailsTableViewController, I can't figure out how to fix my code working again.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DestEntryDetailedTableViewController : DetailsTableViewController = segue.destinationViewController as! DetailsTableViewController }
What's the best and acceptable way of doing this? How to connect direktly a connection between TableViewController and DetailsTableViewController?
Note: DetailsTableViewController is not detail view of TableViewController. It just another UITableViewController instance.
Good afternoon,
I'm trying to perform an Unwind segue from a ViewController to the parentViewController, but for some reason it's working but instead of showing the parentViewController wit the UITabBar (because everything is inside a TabBarController) it's showing the parentViewController without the UITabBar.
The question is: How can I perform an "unwind segue" and make visible the UITabBar in my parentViewController?
I perform the first segue (from parent to ViewController) using a segue "present modally" with "current context", and I use this because I need to show the UITabBar also in the ViewController.
That's my first segue (from parent to ViewController):
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showPost" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let navVC = segue.destinationViewController as! UINavigationController
let childVC = navVC.viewControllers.first as! PostViewController
childVC.id_post = posts[indexPath.row].idPost
}
}
}
And that's my unwind segue:
#IBAction func backToPosts(segue:UIStoryboardSegue) {}
Here you are and screenshot of my Storyboard:
Thanks in advance,
Regards.
Try in the viewDidLoad tabBarController.tabBar.hidden = false
have you tried theController.hidesBottomBarWhenPushed
If someone is having the same problem:
I removed the NavigationController after the TabBarController.
Regards
This is a totally dumb question I am sure. I have a viewcontroller that is inside a navigation controller that is inside a TabBarController. Apple says this is the right way to implement that setup. However, how can I prepareForSeque to that complex and send data to the first ViewController.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "presentActionItems" {
println("preparing")
let tabBarController:actionItemsTabController = segue.destinationViewController as! actionItemsTabController
let navigationController = ???
let viewController = ???
viewController.givenURL = actionItemsURL
}
}
I am sure this is an easy question, can I have some help.
Try this in your TabController's viewDidLoad after passing it your data
let firstViewController : UIViewController = self.viewControllers.objectAtIndex(0).topViewController;
firstViewController.this = self.this;
firstViewController.that = self.that;
I am working on a custom camera app where I present 3 viewControllers modally. At each segue, I pass data with prepareForSegue function. My problem is after the work with camera is finished, I need to show 2 more viewControllers which need to be inside a navigationController.
I have realized that If I don't pass any data, the navigation controller works fine. However, when I pass data, the app crashes on runtime. What is the right way of doing this?
Here is my prepare for segue function;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "camera2Crop"{
let controller: CropViewController = segue.destinationViewController as CropViewController
controller.photoTaken = self.photoTaken
}
}
where photoTakenis an UIImage object. Moreover, here is the screenshot from my storyboard where I put the navigationController. I call the prepareForSeguefunction in CustomCameraViewController to segue to CropViewController.
EDIT: I have changed my prepareForSegue to the following code;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "camera2Crop" {
let controller: CustomNavigationController = segue.destinationViewController as CustomNavigationController
controller.photoTaken = self.photoTaken
}
}
Now the app does not crash but I don't know how to send an object through Navigation Controller
let controller: CropViewController = segue.destinationViewController as CropViewController
Double check if segue.destinationViewController is actually the navigation view controller.
If it's the navigation controller, get CropViewController from it:
if segue.identifier == "camera2Crop" {
let navController = segue.destinationViewController as UINavigationController
let controller = navController.viewControllers[0] as CropViewController
controller.photoTaken = self.photoTaken
}
Note you don't have to subclass UINavigationController.