Unable to access index of tabBar item using swift
items is Optional - you can do:
if let items = self.tabBar.items {
println("\(items[1])")
}
or
var cameraTab : UITabBarItem = self.tabBar.items![1] as UITabBarItem
iOS: How to open 0th Index of tabBarItem by clicking 3rd index of tabBarItem
You don't want your view controller's base class to be a UITabBarDelegate. If you were to do that, all of your view controller subclasses would be tab bar delegates. What I think you want to do is to extend UITabBarController
class FiveTabbarController: UITabBarController, UITabBarControllerDelegate {
then, in that class, override viewDidLoad and in there set the delegate property to self:
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
now this class is both a UITabBarDelegate (because UITabBarController implements that protocol), and UITabBarControllerDelegate, and you can override/implement those delegate's methods as desired, such as:
extension FiveTabbarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if let getSelectedIndex = tabBarController.viewControllers?.firstIndex(of: viewController), getSelectedIndex == 2 {
self.selectedIndex = 0
}
}
}
How to get index of selected tab in UITABBAR
https://developer.apple.com/documentation/uikit/uitabbar
UITabBar has 2 properties "items" and "selectedItem", both of type UITabBarItem.
So u can get the index of selected item by getting the index of "selectedItem" in "items".
items.index(of: selectedItem)
How to add a TabBarItem in specific index of UITabBarController?
Here a way to insert new view tab bar item :
import UIKit
class ViewController: UIViewController {
// button and textfields are defined in the storyboard
@IBOutlet var button: UIButton!
var tabbar: UITabBarController?
@IBOutlet var label: UITextField!
@IBOutlet var image: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
label.text = "Four"
image.text = "info.circle.fill"
tabbar = self.tabBarController
}
@IBAction func addVC(button: UIButton) {
// remove keyboard
label.resignFirstResponder()
image.resignFirstResponder()
// check that everything is set
if var tbVC = tabbar?.viewControllers,
let title = label.text,
let imageName = image.text {
// create new view controller
let newVC = UIViewController()
// vreate the tab bar item for the new view controller
let tabBarItem = UITabBarItem(title: title,
image: UIImage(systemName: imageName),
tag: tbVC.count + 1)
newVC.tabBarItem = tabBarItem
// insert new tab in tab bar view controller
tbVC.insert(newVC, at: 2)
// update the teb bar controller
tabbar?.setViewControllers(tbVC, animated: true)
}
}
}
UITabBar(Controller) - Get index of tapped?
The answer depends on whether or not the UITabBar is managed by a UITabBarController or not.
Case 1 - UITabBar is already handled by a UITabBarController
Implement the UITabBarControllerDelegate
protocol. Specifically the tabBarContoller:didSelectViewController:
method. Set an instance of your class that implements the protocol as the delegate
of the UITabBarController
.
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
NSLog(@"Tab index = %u (%u)", (int)indexOfTab);
}
In this case you have to be aware of the special situation where you have enough controllers in the tab controller to cause the "More" tab to be displayed. In that case you'll receive a call to the tabBarController:didSelectViewController:
with a view controller that isn't in the list (it's an instance of an internal UIKit class UIMoreNavigationController). In that case the indexOfTab
in my sample will be NSNotFound
.
Case 2 - UITabBar is NOT already handled by a UITabBarController
Implement the UITabBarDelegate
protocol. Specifically the tabBar:didSelectItem:
method. Set an instance of your class that implements the protocol as the delegate
of the UITabBar
.
- (void)tabBar:(UITabBar *)theTabBar didSelectItem:(UITabBarItem *)item {
NSUInteger indexOfTab = [[theTabBar items] indexOfObject:item];
NSLog(@"Tab index = %u", (int)indexOfTab);
}
EDIT: Modified the method parameter variables to eliminate the OP's compilation warning about tabBarController
being hidden.
Selected Index on Tab Bar not working on Swift
You are saying that
class MainTabBarController: UITabBarController {
so self
refers to a UITabBarController
, but you are setting the selected index of tabBarController
of self
:
self.tabBarController?.selectedIndex = 2
There are no tab bar controllers embedded in self
. self
itself is a tab bar controller! You can just set self.selectedIndex
:
self.selectedIndex = 2
Also, you should do that in viewDidLoad
instead of viewDidAppear
, because viewDidAppear
can happen many times (every time some modal controller is dismissed for example). viewDidLoad
will only be called once.
Get selected index tabbar controller Swift
application.tabBarController
is an optional, this means it can be nil
.
If you are sure it will never be nil
, do this:
var selectedIndex = tabBarController!.selectedIndex
Related Topics
Swiftui Tabbedview Only Shows First Tab's Content
Navigation with Only Back Button and Transparent Background
How to Get the Today's and Tomorrow's Date in Swift 4
How to Handle Homophones in Speech Recognition
Nsnotificationcenter Addobserver in Swift While Call a Private Method
How to Store Array in Nsuserdefault in Swift
Swift - Could Not Cast Value of Type 'Uitabbarcontroller'
How to Give Cornerradius for Uibezierpath
How Do View Types Like Text or Image Conform to the View Protocol in Swiftui
iOS - How to Make the Uiscrolling Indicator Height Same for Any Number of Data
Cropping Avasset Video with Avfoundation Not Working iOS 8
How to Delete an Image from Photolibrary After I Pick It Up Using Uiimagepickercontroller
Show Posts from the Users You Are Following - Swift
Avmutablecomposition Resizing Issue
How to Store JSON Decodable Values into Coredata Using Swift
How to Create a Custom iOS Keyboard