how to callback the array of data to another viewController in iOS Swift

允我心安 提交于 2020-04-15 06:48:50

问题


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:

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...


回答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.




回答2:


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

Here is the link: GLViewPagerController

Other options:

  • Parchment
  • PolioPager



回答3:


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



来源:https://stackoverflow.com/questions/60474304/how-to-callback-the-array-of-data-to-another-viewcontroller-in-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!