my application has three tabs. In tab 1 a view controller lets the user add the name of a contact to tab 2 (A button does this task). As soon as the button is pressed the name gets added to tab 2 and the button is disabled for future operations.
But when the user deletes the name from tab 2 the button should be enabled again. I am able to do this if i reload the tab 1 through the navigation controller but if i switch to tab 1 from 2 the button is still disabled even after the user has deleted the name from tab 2.
how to refresh tab 1 when the user has deleted the name in tab 2
Access Your tab 1 VC from tab 2 VC by TabbarController. and enable the button after deleting the data .
if let tabbarControllers = self.tabBarController?.childViewControllers {
if let firstController = tabbarControllers[0] as? YourFirstVC {
firstController.yourButtonOutlet.isUserInteractionEnabled = true
}
}
And if your tab 1 vc embeded in a navigationcontroller then use.
if let tabbarControllers = self.tabBarController?.childViewControllers {
if let firstControllersNavigation = tabbarControllers[0] as? UINavigationController {
if let firstController = firstControllersNavigation.childViewControllers[0] as? YourFirstVC {
firstController.yourButtonOutlet.isUserInteractionEnabled = true
}
}
}
Related
I am unable to get each tab to show up before it is clicked, I tried setting each one to its own view controller then changing the title that way but it's still not working ( sorry if my formatting is off , this is by first post on here)
thanks
#objc func didTapButton(){
// create and present tab bar controller
let tabBarVC = UITabBarController()
let vc1 = UINavigationController(rootViewController: FirstViewController())
let vc2 = UINavigationController(rootViewController: SecondViewController())
let vc3 = UINavigationController(rootViewController: ThirdViewController())
vc1.title = "My Groups"
vc2.title = "My Events"
vc3.title = "Browse Events"
tabBarVC.setViewControllers([vc1, vc2, vc3], animated: false)
guard let items = tabBarVC.tabBar.items else{
return
}
let images = ["bell", "star", "person"]
for x in 0..<items.count{
items[x].image = UIImage(systemName: images[x])
}
tabBarVC.modalPresentationStyle = .fullScreen
present(tabBarVC, animated: true)
}
Screenshot:
From the doc :
Configuring your tab bar programmatically:
To configure the tab bar associated with a UITabBarController object, configure the view controllers associated with the tab bar controller. The tab bar automatically obtains its items from the tabBarItem property of each view controller associated with the tab bar controller.
To configure tab bar items directly, use the setItems(_:animated:) method of the tab bar itself.
You can not use tabBar.items property directly as you do
I have a storyboard/root viewcontroller as the third tab on my app. On that tab the user selects an image, and writes a caption, which are stored as variables. From the root viewcontroller the user is taken to a preview screen via a segue which passes the variables, i.e. image and text. From this screen the user posts the object. From the post button, I am popping the viewcontroller, and programatically navigating to the first tab of my app. That all works, but the problem is when I then navigate back to the third tab, the stored variables are still there, and I'd like them to be cleared out.
I've seen many posts on keeping the variables, but none on how to reset them. I've tried to use viewWillDissapear but I don't want them cleared when I use the segue because I want the user to be able to go back and make some changes if needed.
UPDATED CODE WITH CORRECT ANSWER
#IBAction func postButtonPressed(_ sender: Any) {
PostFunction.createPost(image: self.postImage, postText: self.hashtag) { (true) in
self.tabBarController?.selectedIndex = 0
let tab3 = self.tabBarController!.viewControllers![2] as! UINavigationController
let vc = tab3.viewControllers.first as! PostHomeVC
vc.clear()
self.navigationController?.popViewController(animated: false)
}
}
Any suggestions on how to "reset" the rootviewcontroller from the above method?
Thanks!
I am popping the viewcontroller, and programatically navigating to the first tab of my app
before you do the programmatic switch to the first tab do
let tab3 = self.tabBarController.viewControllers[2] as! UINavigationController
let vc = tab3.viewControllers.first as! VCName
vc.clear()
or
let vc = self.navigationController!.viewControllers.first as! VCName
vc.clear()
and write that clear method inside the vc as you need
this is my procedural scheme
i have a tab controllore with 3 items and into 1 items i have a navigation controller than into this i have a popup
i need to go back from my popup to my tab bar controller with index 2
this is my code but doesn't work.
let TabViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabViewController") as! TabViewController
self.present(TabViewController, animated: true)
I have to be able to go back from my popup, to my tab bar controller directly to the page with item 2
You can try
self.dismiss(animated:true) {
let TabViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabViewController") as! TabViewController
TabViewController.selectedIndex = 2
UIApplication.shared.keyWindow?.rootViewController = TabViewController
}
I want to show a tab bar controller along with sidebar menu in homepage (Main Page). After a successful login if I call the delegate to main page with sidebar menu then tab bar does not load and if i call the tab bar controller after login then sidebar menu does not work, I am confused on how to call the sidebar menu delegate and tab bar controller together so it may show both together (Sidebar menu and tab bar together on main page) in swift ios9.
Here is my code in App Delegate that build my sidebar menu
func buildNavigationDrawer()
{
// Navigate to Protected page
let mainStoryBoard:UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
// Create View Controllers
let mainPage = mainStoryBoard.instantiateViewControllerWithIdentifier("MainPageViewController") as! MainPageViewController
let leftSideMenu:LeftSideViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("LeftSideViewController") as! LeftSideViewController
let rightSideMenu:RightSideViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("RightSideViewController") as! RightSideViewController
let mainPageNav = UINavigationController(rootViewController:mainPage)
let leftSideMenuNav = UINavigationController(rootViewController:leftSideMenu)
let rightSideMenuNav = UINavigationController(rootViewController:rightSideMenu)
drawerContainer = MMDrawerController(centerViewController: mainPageNav, leftDrawerViewController: leftSideMenuNav, rightDrawerViewController: rightSideMenuNav)
drawerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView
drawerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView
window?.rootViewController = drawerContainer
}
http://i.stack.imgur.com/05AQz.png
Click Here to view Image
Thank you.
Is it possible to hide or disable a tab bar item on a tab bar throughout the entire app for a certain use case?
Example:
While the user is logged in, and they do not have a Role of 'manager', the last tab bar item will be hidden throughout the app. When they log in again as a manager the last tab bar will be enabled and not hidden.
If you are inside the source file of the UITabBarController, just add below code in viewDidLoad method to disable last item
Also below code assumes you are having UITabBarItem items in tab bar. Otherwise you know what type of item is it so you can cast accordingly
if let items = tabBar.items as? [UITabBarItem] {
if items.count > 0 {
let itemToDisable = items[items.count - 1]
itemToDisable.enabled = false
}
}
Better code (in Swift 4):
tabBar.items?.forEach { $0.isEnabled = false }
Better solution (in Swift 5)
tabBarControlled?.tabBar.items?[2].isEnabled = isManager
Swift 5 one liner
let n = 2. //The tab number to disable
self.tabBarController!.tabBar.items![n].isEnabled = false