Change view in tab bar controller - ios

I have some problem at creating programmatically tab bar. I create class where create tab bar:
override func viewWillAppear(animated: Bool) {
let mondayTab = MondayTableViewController()
mondayTab.tabBarItem.title = "Monday"
mondayTab.tabBarItem.image = UIImage(named: "")
let tuesdayTab = TuesdayTableViewController()
tuesdayTab.tabBarItem.title = "Tuesday"
tuesdayTab.tabBarItem.image = UIImage(named: "")
let wednesdayTab = WednesdayTableViewController()
wednesdayTab.tabBarItem.title = "Wednesday"
wednesdayTab.tabBarItem.image = UIImage(named: "")
let thursdayTab = TuesdayTableViewController()
thursdayTab.tabBarItem.title = "Thursday"
thursdayTab.tabBarItem.image = UIImage(named: "")
let fridayTab = TuesdayTableViewController()
fridayTab.tabBarItem.title = "Friday"
fridayTab.tabBarItem.image = UIImage(named: "")
let tabBarController = [mondayTab, tuesdayTab, wednesdayTab, thursdayTab, fridayTab]
self.viewControllers = tabBarController
}
In viewDidLoad I try to change starting view, using something like this:
tabBarController?.selectedIndex = 1
but it not working...

Related

UITabBarContoller, disappears last item from time to time

I have my custom TabBarController as usual, which contains 8 viewController.
class STTabBarController: UITabBarController,UITabBarControllerDelegate {
let tabBarOrderKey = "tabBarOrderKey"
private var messangerNavigationController: UINavigationController!
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
configureViewControllers()
setUpTabBarItemTags()
getSavedTabBarItemsOrder()
}
func configureViewControllers() {
let clientsController = STClientsViewController(nibName: "STClientsViewController", bundle: nil)
let clientNavigationController = UINavigationController(rootViewController: clientsController)
clientsController.title = "Clients"
clientNavigationController.tabBarItem.image = UIImage(named: "Client")
let openHouseController = STOpenHouseViewController(nibName: "STOpenHouseViewController", bundle: nil)
let openHouseNavigationController = UINavigationController(rootViewController: openHouseController)
openHouseController.title = "Open House"
openHouseNavigationController.tabBarItem.image = UIImage(named: "OpenHouse")
let performanceController = STChartsViewController(nibName: "STChartsViewController", bundle: nil)
let performanceNavigationController = UINavigationController(rootViewController: performanceController)
performanceController.title = "Performance"
performanceNavigationController.tabBarItem.image = UIImage(named: "Performance")
let calculatorsController = STCalculatorsViewController(nibName: "STCalculatorsViewController", bundle: nil)
let calculatorsNavigationController = UINavigationController(rootViewController: calculatorsController)
calculatorsController.title = "Calculators"
calculatorsNavigationController.tabBarItem.image = UIImage(named: "Calculators")
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let communityViewController = storyBoard.instantiateViewController(withIdentifier: "Navigation")
communityViewController.title = "Community"
communityViewController.tabBarItem.image = UIImage (named:"Community")
let industryProfessionalsController = STIndustryProfessionalsViewController(nibName: "STIndustryProfessionalsViewController", bundle: nil)
let industryProfessionalsNavigationController = UINavigationController(rootViewController: industryProfessionalsController)
industryProfessionalsController.title = "Vendors"
industryProfessionalsNavigationController.title = "Vendors"
industryProfessionalsNavigationController.tabBarItem.image = UIImage(named: "Industry-professionals")
let agentResourcesController = STAgentResourcesViewController(nibName: "STAgentResourcesViewController", bundle: nil)
let agentResourcesNavigationController = UINavigationController(rootViewController: agentResourcesController)
agentResourcesController.title = "Resources"
agentResourcesNavigationController.title = "Resources"
agentResourcesNavigationController.tabBarItem.image = UIImage(named: "Agent-Resources")
let settingsController = STSettingsViewController(nibName: "STSettingsViewController", bundle: nil)
let settingsNavigationController = UINavigationController(rootViewController: settingsController)
settingsController.title = "Settings"
settingsNavigationController.tabBarItem.image = UIImage(named: "Settings")
let coachController = STCoachsCornerViewController(nibName: "STCoachsCornerViewController", bundle: nil)
let coachNavigationController = UINavigationController(rootViewController: coachController)
coachController.navigationItem.title = "Action Plan"
coachNavigationController.tabBarItem.title = "Plan"
coachNavigationController.tabBarItem.image = UIImage(named: "Plan")
self.viewControllers = [clientNavigationController ,performanceNavigationController,calculatorsNavigationController, coachNavigationController,industryProfessionalsNavigationController,agentResourcesNavigationController,openHouseNavigationController, settingsNavigationController]
tabBar.isTranslucent = false
let topBorder = CALayer()
topBorder.frame = CGRect(x: 0, y: 0, width: 1000, height: 0.5)
topBorder.backgroundColor = UIColor.returnRGBColor(r: 229, g: 231, b: 235, alpha: 1).cgColor
tabBar.layer.addSublayer(topBorder)
tabBar.clipsToBounds = true
}
func setUpTabBarItemTags() {
var tag = 0
if let viewControllers = viewControllers {
for view in viewControllers {
view.tabBarItem.tag = tag
tag += 1
}
}
}
func getSavedTabBarItemsOrder() {
var newViewControllerOrder = [UIViewController]()
if let initialViewControllers = viewControllers {
if let tabBarOrder = UserDefaults.standard.object(forKey: tabBarOrderKey) as? [Int] {
for tag in tabBarOrder {
newViewControllerOrder.append(initialViewControllers[tag])
}
setViewControllers(newViewControllerOrder, animated: false)
}
}
}
func tabBarController(_ tabBarController: UITabBarController, didEndCustomizing viewControllers: [UIViewController], changed: Bool) {
var orderedTagItems = [Int]()
if changed {
for viewController in viewControllers {
let tag = viewController.tabBarItem.tag
orderedTagItems.append(tag)
}
UserDefaults.standard.set(orderedTagItems, forKey: tabBarOrderKey)
}
}
And I met the problem when I start my on different devices and from time to time it can hide Settings(last) item in "More" tab.This look kinda ridiculous because code is straightforward and simple as you see and I dont know what can be wrong here.
Does smb know what can it be? Thanks
The problem was in saving order of this tabs to User Defaults.
I had 7 controllers, but when was trying to save it, saved was only 6.
Here is final code of saving function:
func getSavedTabBarItemsOrder() {
var newViewControllerOrder = [UIViewController]()
if let initialViewControllers = viewControllers {
if let tabBarOrder = UserDefaults.standard.object(forKey: tabBarOrderKey) as? [Int] {
for tag in tabBarOrder {
newViewControllerOrder.append(initialViewControllers[tag])
}
let difference = Set(initialViewControllers).subtracting(newViewControllerOrder)
newViewControllerOrder.append(contentsOf: difference)
setViewControllers(newViewControllerOrder, animated: false)
}
}
}

Switch tab bar include sending data

How can i change the tab bar? i know this post seems duplicate but i cant find any exist question that similar to me. Right now my current
selectedIndex = 0
so i want to make it go to tab number 3 which is
selectedIndex = 2
But i also want to send data from currentView to nextView. if i using push+selectedindex it will go to tab 3 but push the view from selectedindex = 0, and there is no data send to selectedIndex = 2
My current code
func redeemBtnPressed(_ sender: UIButton) {
let selectedRedeemBtnInfo = fixedGridInfo[sender.tag] as! Dictionary<String, AnyObject>
sender.showsTouchWhenHighlighted = true
let storyboard = UIStoryboard(name: "FlightExploration", bundle: nil)
let searchFlightVC = storyboard.instantiateViewController(withIdentifier: "SearchFlightVC") as! SearchFlightViewController
var newFlightType = String()
if "\(selectedRedeemBtnInfo["FlightType"]!)" == "Return" {
newFlightType = "Round"
} else {
newFlightType = "One"
}
searchFlightVC.flightType = newFlightType
searchFlightVC.fromHome = true
searchFlightVC.departure = "\(selectedRedeemBtnInfo["Departure"]!) (\(selectedRedeemBtnInfo["DepartureCityCode"]!)"
searchFlightVC.arrival = "\(selectedRedeemBtnInfo["Destination"]!) (\(selectedRedeemBtnInfo["DestinationCityCode"]!)"
self.navigationController?.pushViewController(searchFlightVC, animated: true)
tabBarController?.selectedIndex = 2
}
You can try this to send the data to that UIViewController which is a ViewController of UITabBarController
var yourViewController : TempViewController
if let arrController = tabBarController?.viewControllers {
for vc in arrController {
if vc is TempViewController {
yourViewController = vc as! TempViewController
}
}
}
yourViewController.yourData = dataToPass
tabBarController?.selectedIndex = 2
Modified from Rajat's answer seems helped me to solved the issue
func redeemBtnPressed(_ sender: UIButton) {
let selectedRedeemBtnInfo = fixedGridInfo[sender.tag] as! Dictionary<String, AnyObject>
sender.showsTouchWhenHighlighted = true
var newFlightType = String()
if "\(selectedRedeemBtnInfo["FlightType"]!)" == "Return" {
newFlightType = "Round"
} else {
newFlightType = "One"
}
if let arrController = tabBarController?.viewControllers {
for vc in arrController {
if vc.childViewControllers[0] is SearchFlightViewController {
let displayViewController = vc.childViewControllers[0] as! SearchFlightViewController
let _ = displayViewController.navigationController?.popToRootViewController(animated: true)
//displayViewController.flightType = newFlightType
displayViewController.flightTypeFromHome = newFlightType
displayViewController.fromHome = true
displayViewController.departure = "\(selectedRedeemBtnInfo["Departure"]!) (\(selectedRedeemBtnInfo["DepartureCityCode"]!)"
displayViewController.arrival = "\(selectedRedeemBtnInfo["Destination"]!) (\(selectedRedeemBtnInfo["DestinationCityCode"]!)"
displayViewController.flightType = "Round"
displayViewController.departureDateLbl = "Select One"
displayViewController.passenger = "1 Adult"
displayViewController.adultCount = 1
displayViewController.childCount = Int()
displayViewController.infantCount = Int()
tabBarController?.selectedIndex = 2
tabBarController?.tabBar((tabBarController?.tabBar)!, didSelect: (tabBarController?.tabBar.items?[2])!)
}
}
}
}
Rajat's answer Switch Tab Bar and Pass data, in Swift 4.2, iOS 11 need some changes to be done:
func switchToTab2(){}
var yourViewController = MyTab2ViewController()
if let arrController = self.tabBarController?.viewControllers {
for vc in arrController {
if vc is MyTab2ViewController {
yourViewController = vc as! MyTab2ViewController
yourViewController.productTitle = "Title"
self.tabBarController?.selectedIndex = 1 /// tabs start from 0
}
}
}
}
MyTab2ViewController is your viewcontroller connected to tab bar with index 1. (first tab index: 0)

Swift 3 increment increment tab bar badge [duplicate]

#IBAction func addToCart(sender: AnyObject) {
let itemObjectTitle = itemObject.valueForKey("itemDescription") as! String
let alertController = UIAlertController(title: "Add \(itemObjectTitle) to cart?", message: "", preferredStyle: .Alert)
let yesAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) { (action) in
var tabArray = self.tabBarController?.tabBar.items as NSArray!
var tabItem = tabArray.objectAtIndex(1) as! UITabBarItem
let badgeValue = "1"
if let x = badgeValue.toInt() {
tabItem.badgeValue = "\(x)"
}
}
I don't know why I can't just do += "(x)"
Error:
binary operator '+=' cannot be applied to operands of type 'String?' and 'String'
I want it to increment by 1 each time the user selects "Yes". Right now obviously it just stays at 1.
You can try to access the badgeValue and convert it to Integer as follow:
Swift 2
if let badgeValue = tabBarController?.tabBar.items?[1].badgeValue,
nextValue = Int(badgeValue)?.successor() {
tabBarController?.tabBar.items?[1].badgeValue = String(nextValue)
} else {
tabBarController?.tabBar.items?[1].badgeValue = "1"
}
Swift 3 or later
if let badgeValue = tabBarController?.tabBar.items?[1].badgeValue,
let value = Int(badgeValue) {
tabBarController?.tabBar.items?[1].badgeValue = String(value + 1)
} else {
tabBarController?.tabBar.items?[1].badgeValue = "1"
}
To delete the badge just assign nil to the badgeValue overriding viewDidAppear method:
override func viewDidAppear(animated: Bool) {
tabBarController?.tabBar.items?[1].badgeValue = nil
}
Works with Swift 2:
let tabController = UIApplication.sharedApplication().windows.first?.rootViewController as? UITabBarController
let tabArray = tabController!.tabBar.items as NSArray!
let alertTabItem = tabArray.objectAtIndex(2) as! UITabBarItem
if let badgeValue = (alertTabItem.badgeValue) {
let intValue = Int(badgeValue)
alertTabItem.badgeValue = (intValue! + 1).description
print(intValue)
} else {
alertTabItem.badgeValue = "1"
}

Increment tab bar badge w/ UIAlertAction swift?

#IBAction func addToCart(sender: AnyObject) {
let itemObjectTitle = itemObject.valueForKey("itemDescription") as! String
let alertController = UIAlertController(title: "Add \(itemObjectTitle) to cart?", message: "", preferredStyle: .Alert)
let yesAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) { (action) in
var tabArray = self.tabBarController?.tabBar.items as NSArray!
var tabItem = tabArray.objectAtIndex(1) as! UITabBarItem
let badgeValue = "1"
if let x = badgeValue.toInt() {
tabItem.badgeValue = "\(x)"
}
}
I don't know why I can't just do += "(x)"
Error:
binary operator '+=' cannot be applied to operands of type 'String?' and 'String'
I want it to increment by 1 each time the user selects "Yes". Right now obviously it just stays at 1.
You can try to access the badgeValue and convert it to Integer as follow:
Swift 2
if let badgeValue = tabBarController?.tabBar.items?[1].badgeValue,
nextValue = Int(badgeValue)?.successor() {
tabBarController?.tabBar.items?[1].badgeValue = String(nextValue)
} else {
tabBarController?.tabBar.items?[1].badgeValue = "1"
}
Swift 3 or later
if let badgeValue = tabBarController?.tabBar.items?[1].badgeValue,
let value = Int(badgeValue) {
tabBarController?.tabBar.items?[1].badgeValue = String(value + 1)
} else {
tabBarController?.tabBar.items?[1].badgeValue = "1"
}
To delete the badge just assign nil to the badgeValue overriding viewDidAppear method:
override func viewDidAppear(animated: Bool) {
tabBarController?.tabBar.items?[1].badgeValue = nil
}
Works with Swift 2:
let tabController = UIApplication.sharedApplication().windows.first?.rootViewController as? UITabBarController
let tabArray = tabController!.tabBar.items as NSArray!
let alertTabItem = tabArray.objectAtIndex(2) as! UITabBarItem
if let badgeValue = (alertTabItem.badgeValue) {
let intValue = Int(badgeValue)
alertTabItem.badgeValue = (intValue! + 1).description
print(intValue)
} else {
alertTabItem.badgeValue = "1"
}

Randomly select a UIImage

I need to pick one of these dots. I add them all before viewDidLoad, I need it to pick a random one. My current code returns the error cannot assign to 'openingScreenDynamicDot in 'self'. How is this fixed?
CODE:
let openingScreenDynamicDot = UIImage()
let dotOne = UIImage(named: "dot1.png")
let dotTwo = UIImage(named: "dot2.png")
let dotThree = UIImage(named: "dot3.png")
let dotFour = UIImage(named: "dot4.png")
let dotFive = UIImage(named: "dot5.png")
let dotSix = UIImage(named: "dot6.png")
let dotSeven = UIImage(named: "dot7.png")
let dotEight = UIImage(named: "dot8.png")
let dotNine = UIImage(named: "dot9.png")
let dotTen = UIImage(named: "dot10.png")
let dotEleven = UIImage(named: "dot11.png")
let dotTwelve = UIImage(named: "dot12.png")
let dotThirteen = UIImage(named: "dot13.png")
var imageNumber = arc4random()%13
override func viewDidLoad() {
let theRandomImages = [dotOne, dotTwo, dotThree, dotFour, dotFive, dotSix, dotSeven, dotEight, dotNine, dotTen, dotEleven, dotTwelve, dotThirteen]
openingScreenDynamicDot = theRandomImages.objectAtIndex(imageNumber)
}
You declare constants with the let keyword and variables with the var keyword.
So change let openingScreenDynamicDot to var openingScreenDynamicDot
also, a swift native array does not have a objectAtIndex method so..
change
openingScreenDynamicDot = theRandomImages.objectAtIndex(imageNumber)
to
openingScreenDynamicDot = theRandomImages[imageNumber]
You can try this code to generate one of your random "dots".
override func viewDidLoad(){
let openingScreenDynamicDot.image = UIImage(named: "dot\(arc4random_uniform(13) + 1).png")
}
This is called "String Interpolation". For more info, Click here.
I hope this can help you if you are still having problems.

Resources