[swift5] How to execute processing when tabBar is tapped

Let's use didSelect

https://developer.apple.com/documentation/uikit/uitabbardelegate/1623463-tabbar Official Apple reference.

If you want to do something when you tap the tabBar, use dedSelect.

ViewController.swift


//Action when tapping tabbar
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
  switch item.title {
    case "news":
      print("Tap a news item")
    case "weather":
      print("Tap the weather item")
    case "Search":
      print("Tap the search item")
    default: break
    }
}

In this sample code, the process is divided for each title of tabBar item in the switch statement. ʻItem.title` means that.

The tabBar is managed by the numbers tag [0.1.2 ...] from the left, but when a third party sees the code, it's more like an item. Since I think that it is easier to understand if it is branched by title, it is ʻitem.title`.

Recommended Posts

[swift5] How to execute processing when tabBar is tapped
How to execute action attribute processing only when the confirmation dialog with h: commandButton is OK
How to execute processing before and after docker-entrypoint.sh
How to execute tasks in parallel in Swift in Swift Package
[swift] How to control the behavior when the back button of NavigationBar is pressed
[Swift] How to use UserDefaults
How to use Swift UIScrollView
[Swift] What is asynchronous processing?
[Swift] Processing to share screenshots
[Swift] How to display when the quiz app answers correctly [Beginner]
[Processing × Java] How to use variables
[Swift] How to use SwiftLint (cocoapods)
[Swift] How to use Unwind segue
[Swift] How to send a notification
[Swift] How to replace multiple strings
[Processing × Java] How to use arrays
Practice to use when you want to execute different processing groups serially
How to execute Ruby irb (interactive ruby)
How to write the view when Vue is introduced in Rails?
[Note] How to expand storage when Sakura's VPS is scaled up (CentOS 7)
[Swift] How to connect TabBar with Storyboard Reference and also use NavigationController
[Ruby] How to prevent errors when nil is included in the operation
[swift5] How to change the color of TabBar or the color of item of TabBar with code
How to execute a contract using web3j
[swift5] How to specify color in hexadecimal
[Swift] How to fix Label in UIPickerView
[Swift] How to use Tab Bar Controller
How to call Swift 5.3 code from Objective-C
How to resolve errors when installing Rails 5.1.3
[Swift] How to implement the countdown function
[Swift5] How to create a splash screen
[Processing × Java] How to use the function
When UITextView is not displayed (Swift, Xcode)
[Swift5] How to implement animation using "lottie-ios"
How to implement asynchronous processing in Outsystems
How to overwrite Firebase data in Swift
[Swift] How to use one option alert
How to execute multiple commands in docker-compose.yml
How to redirect to http-> https when SSL is enabled in Rails × Heroku environment
[Swift5] Problem that Cell cannot be tapped when TextView is placed in TableViewCell
[Swift5] How to compare specific times to determine if it is before or after
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
[Heroku] How to solve when an error is displayed on git push heroku master
How to set when "The constructor Empty () is not visible" occurs in junit
How to disable user operations during asynchronous processing
[Rails] How to write when making a subquery
[Swift 5] Processing to close the keyboard on UITableView
Points to review when Rubocop is slow to run
How to execute and mock methods using JUnit
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
[Ethereum] How to execute a contract using web3j-Part 2-
How to add sound in the app (swift)
[Swift] How to link the app with Firebase
How to delete the database when recreating the application
[Swift UI] How to disable ScrollsToTop of ScrollView
How to use scope and pass processing (Jakarta)
[Swift] How to implement the fade-in / out function
How to log in automatically when Ubuntu restarts
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
How to fix the problem that the upper half is cut off when using UITabBar