【发布时间】:2020-03-01 10:51:53
【问题描述】:
在 createCardVC 中,我使用 carbonKit 库来显示标签栏。最初,使用静态数据加载的数据数组,但现在我尝试使用来自 webView javascript postMessage 的数据数组。
- 加载 createCardVC 时,将加载第一个选项卡为 webViewVC 的 carbonKit。
- 在 webView 中,从 postMessage 中它将列出数字菜单项以显示标签栏菜单。
- 此处的选项卡值是动态的,将从 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)
}
}
}
}
我的问题是:
- 如何将选项卡值从 webView 返回到 CreateCardVC?
- 如何在 carbonKit 中显示动态标签?
- 如何使用相同的 webViewController 为下一个选项卡动态更改 ViewController,并且 url 将从 webViewController 返回。
任何帮助都非常感谢...
【问题讨论】:
标签: ios swift webview callback carbonkit