2

In createCardVC, I have used the carbonKit library to show tab bar. Initially, the array of data loaded using static data but now I am trying to use an array of data from webView javascript postMessage.

  1. When createCardVC is loaded the carbonKit of the first tab is webViewVC will be loaded.
  2. In webView, From postMessage it will list number menu items to show tab bar menu.
  3. Here tabs values are dynamic and which will return from webView postMessage.

Here is the clear picture:

enter image description here

Here is the code createCardVC:

  override public func viewDidLoad() {
    super.viewDidLoad()

    carbonTabSwipeNavigation = CarbonTabSwipeNavigation(items: ["Basic Details"], delegate: self)
    carbonTabSwipeNavigation.insert(intoRootViewController: self, andTargetView: infoView)
    carbonTabSwipeNavigation.toolbar.barTintColor = UIColor.white
    carbonTabSwipeNavigation.setSelectedColor(UIColor.black)
    carbonTabSwipeNavigation.setIndicatorColor(UIColor(hexString: "#363794")) 

}//viewdidload

func onUserAction(data: String)
{
    print("Data received: \(data)")
}


func sampleDelegateMethod(arg: Bool,completion: (Bool) -> ()){

    completion(arg)

    let singleTon = SingletonClass()
            print(singleTon.sharedInstance.dataText)
}


@IBAction func backBtn(_ sender: Any) {

   _ = navigationController?.popViewController(animated: true)
    navigationController?.setNavigationBarHidden(false, animated: true)
    tabBarController?.tabBar.isHidden = false


}



public init() {

         super.init(nibName: "CreateCardViewController", bundle: Bundle(for: CreateCardViewController.self))
     }

     required init?(coder aDecoder: NSCoder) {

         fatalError("init(coder:) has not been implemented")

     }

public func carbonTabSwipeNavigation(_ carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAt index: UInt) -> UIViewController {


    return firstView()

   }
func firstView() -> UIViewController {



    let cont = WebViewController()
    self.tabContView?.addChild(cont)
    self.tabContView?.view.addSubview(cont.view)

    cont.didMove(toParent: tabContView)
    let authToken = UserDefaults.standard.string(forKey: "authToken")

    cont.formKey = formKey
    print("cont vl", formKey ?? "")
    cont.processInstanceId = processInstanceId
    cont.authTokenValue = authToken
    cont.fullFormKey = fullFormKey
    cont.taskIdValue = TaskIdValue


    return cont

}

Here is the code for webView:

 public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {

    if message.name == "jsHandler" {
       //  print(message.body)

    } else if message.name == "tabForm" {


        print("dynamic tabs value::::",message.body)

        let tabs = message.body
        let jsonString = JSONStringify(value: tabs as AnyObject)

            if let jsonData = jsonString.data(using: .utf8) {
              do {
                let decoder = JSONDecoder()
                let mainObject = try decoder.decode(DynamicTabsModel.self, from: jsonData)

                print("tab model:::", mainObject)

                createCardVC?.onUserAction(data: "The quick brown fox jumps over the lazy dog")
                delegate?.onPizzaReady(type: "Pizza di Mama")
                createCardVC?.sampleDelegateMethod(arg: true, completion: { (success) -> Void in

                    print("Second line of code executed")
                    if success { // this will be equal to whatever value is set in this method call

                        createCardVC?.onUserAction(data: "The quick brown fox jumps over the lazy dog")
                        delegate?.onPizzaReady(type: "Pizza di Mama")

                        let singleTon = SingletonClass()
                        singleTon.sharedInstance.dataText = "store data"

                        print("delegate method ::: true")


                    } else {
                         print("delegate method ::: false")
                    }
                })

                print("called delegate")

              } catch {

                print(error.localizedDescription)

              }
            }



    }
   }

My question is:

  1. How to return tab values from webView to CreateCardVC?
  2. How to show dynamic tabs in carbonKit?
  3. How to change dynamically ViewController for next tab using the same webViewController and url will return from webViewController.

Any help much appreciated pls...

PvUIDev
  • 95
  • 1
  • 9
  • 38

3 Answers3

1

Your approach is wrong for using CarbonKit. You have to provide list of tabs for CarbonKit while initialising it.

What you are trying to achieve is initialise CarbonKit with only one tab and then add more tabs as required, that is not supported by CarbonKit.

What you should do is get list of tabs before creating CarbonTabSwipeNavigation. If you don't have any option other than using WebView to get list of tabs, load your WebView first and get list of Tabs and then create CarbonTabSwipeNavigation.

Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
  • Thanks for answering. Does any other third party their for my approach. – PvUIDev Mar 24 '20 at 16:15
  • Don't know any such library but one workaround I can suggest is First load CarbonTabSwipeNavigation with only one tab with Webview, get number of tabs from there and post one local notification (with tabs as userData) and can recreate whole CarbonTabSwipeNavigation with new tabs – Yuvrajsinh Mar 26 '20 at 13:01
1

You can try using "GLViewPagerController" it is more dynamic and API is similar to "UITableView"

Here is the link: GLViewPagerController

Other options:

Tim Kozak
  • 4,026
  • 39
  • 44
1

I strongly recommend to use Parchemnt. It uses Hashable.

https://github.com/rechsteiner/Parchment

See infinite datasource method for dynamic usage. Download the demo project and checkout Calendar Example.

https://github.com/rechsteiner/Parchment/blob/master/Documentation/infinite-data-source.md

CrackIt
  • 606
  • 1
  • 5
  • 15