如何在 iOS Swift 中将数据数组回调到另一个 viewController

2024-01-07

在createCardVC中,我使用carbonKit库来显示标签栏。最初,使用静态数据加载的数据数组,但现在我尝试使用来自 webView javascript postMessage 的数据数组。

  1. 当 createCardVC 加载时,第一个选项卡 webViewVC 的 CarbonKit 将被加载。
  2. 在 webView 中,从 postMessage 它将列出数字菜单项以显示选项卡栏菜单。
  3. 这里的选项卡值是动态的,将从 webView postMessage 返回。

这是清晰的图片:

这是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

}

下面是 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)

              }
            }



    }
   }

我的问题是:

  1. 如何将选项卡值从 webView 返回到 CreateCardVC?
  2. 如何在carbonKit中显示动态选项卡?
  3. 如何使用相同的 webViewController 动态更改下一个选项卡的 ViewController,并且 url 将从 webViewController 返回。

任何帮助非常感谢...


您使用 CarbonKit 的方法是错误的。您必须在初始化时提供 CarbonKit 的选项卡列表。

您想要实现的目标是仅使用一个选项卡初始化 CarbonKit,然后根据需要添加更多选项卡,这是 CarbonKit 不支持的。

您应该做的是在创建之前获取选项卡列表CarbonTabSwipeNavigation。如果除了使用 WebView 获取选项卡列表之外没有任何选项,请先加载 WebView 并获取选项卡列表,然后创建CarbonTabSwipeNavigation.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 iOS Swift 中将数据数组回调到另一个 viewController 的相关文章

随机推荐