I have 4 Tab Bar Items. When these are clicked, I want a different website to open.
I tried the examples on the internet but couldn't get it to work. I want 4 different urls to be opened when 4 different buttons are pressed on a single page. I can't seem to catch the click event. I check the logs with print but it is not responding.
My codes. Does it not respond when clicked?
//
// ViewController.swift
// Oto
//
// Created by Emre Taner Çetinkaya on 19.08.2022.
//
import UIKit
import WebKit
import Foundation
class ViewController: UIViewController, WKNavigationDelegate, UITabBarControllerDelegate {
@IBOutlet weak var tabBar: UITabBar!
@IBOutlet weak var profileButton: UITabBarItem!
@IBOutlet weak var discountButton: UITabBarItem!
@IBOutlet weak var urgencyButton: UITabBarItem!
@IBOutlet weak var homeButton: UITabBarItem!
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.delegate = self
setUpSetting();
loadData(link: "https://oto.com/")
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
switch tabBarIndex {
case 0:
loadData(link: "https://oto.com/")
print("press 0");
case 1:
loadData(link:"oto.com//sayfa/acil-acil/")
case 2:
loadData(link: "https://oto.com//sayfa/fiyati-dusenler/")
case 3:
loadData(link: "https://oto.com//sayfa/giris-yap/")
default:
loadData(link: "https://oto.com/")
}
}
}
func setUpSetting () {
let tabBarAppearance = UITabBarAppearance()
let tabBarItemAppearance = UITabBarItemAppearance()
tabBarItemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
tabBarItemAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
tabBarAppearance.backgroundColor = hexStringToUIColor(hex: "3f475f")
tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
tabBar.standardAppearance = tabBarAppearance
tabBar.scrollEdgeAppearance = tabBarAppearance
}
func hexStringToUIColor(hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt64 = 0
Scanner(string: cString).scanHexInt64(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
func loadData(link : String) {
let url = URL(string: link)!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}