0

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.

Storyboard

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
        
    }

    
    
    
}
  • Need to conform to UITabBarControllerDelegate, not table view. Set delegate. – cora Aug 21 '22 at 14:06
  • @cora I edited the code as you said. Still not interacting. I edited the first post, you can check it out. – Emre Taner Çetinkaya Aug 21 '22 at 14:46
  • https://stackoverflow.com/questions/33837475/detect-when-a-tab-bar-item-is-pressed – cora Aug 21 '22 at 15:33
  • Does this answer your question? [Detect when a tab bar item is pressed](https://stackoverflow.com/questions/33837475/detect-when-a-tab-bar-item-is-pressed) – burnsi Aug 26 '22 at 16:16

0 Answers0