【问题标题】:Change WKWebview URL with button in different ViewController使用不同 ViewController 中的按钮更改 WKWebview URL
【发布时间】:2016-06-24 21:30:57
【问题描述】:

我对 iOS 开发非常陌生,想尝试使用 webview 构建一个混合 iOS 应用程序。我一直在学习,并在此处或其他网站上找到了我需要的大多数答案,但我已经到了卡住的地步。

我在主 ViewController 中设置了 webview,一切正常。我正在使用一个库来创建一个效果很好的侧边菜单。然而,由于库的工作方式,菜单内置在另一个 ViewController 中。我想用将在 web 视图中加载的链接填充侧面菜单。这可能吗?

编辑:我可能还应该提到我正在迅速完成这一切。

EDIT2:这是我用于侧边菜单的框架的链接:https://github.com/jonkykong/SideMenu

该框架几乎允许您使用 tableview 构建自己的视图。

【问题讨论】:

  • 您能告诉我们,您正在使用哪个框架以及在哪里可以找到它的文档?应该可以使用委托协议或通知
  • @FelixSFD 感谢您的快速回复。我已经编辑了问题,以包含指向我正在使用的侧边菜单框架的链接。

标签: ios xcode swift webview wkwebview


【解决方案1】:

我不知道我是否正确理解了您的问题。以下是我对您的问题的看法:

你的 MenuViewController 包含一个 UITableView,它是一个菜单列表,当一个单元格被选中时,你的 MainViewController 的 webview 会加载一个链接 URL,但是这个 URL 是放在 MenuViewController 中的,对吧?

如果这是您的问题,您可以通过以下解决方案解决:

MainViewController 添加通知观察者,MenuViewController 发布通知

// MainViewController
class ViewController: UIViewController {
let webView = UIWebView()

override func viewDidLoad() {
    super.viewDidLoad()
    webView.frame = self.view.bounds
    self.view.addSubview(webView)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleNotification(_:)), name: "OpenURL", object: nil)
}

func handleNotification(notification:NSNotification) {
    if let url = notification.userInfo?["url"] {
        webView.loadRequest(NSURLRequest(URL: url as! NSURL))
    }
}
}

// MenuViewController
class MenuViewController: UIViewController, UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let userInfo = ["url" : your_url]
    NSNotificationCenter.defaultCenter().postNotificationName("OpenURL", object: nil, userInfo: userInfo)
}
}

更新:

如何使用 UITableView

class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView(frame: CGRectZero, style: .Plain)
let urls = ["https://google.com", "https://youtube.com", "http://stackoverflow.com/"]
let cellIdentifier = "CellIdentifier"

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.frame = view.bounds
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)

    view.addSubview(tableView)
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return urls.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
    cell.textLabel?.text = urls[indexPath.row]
    return cell
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let url = NSURL(string: urls[indexPath.row])!
    let userInfo = ["url" : url]
    NSNotificationCenter.defaultCenter().postNotificationName("OpenURL", object: nil, userInfo: userInfo)
}
}

【讨论】:

  • 是的,这是我的问题。我很快尝试了这个,但是当我单击按钮打开侧边菜单时,我收到了这个错误:“libc++abi.dylib:以 NSException 类型的未捕获异常终止。”我知道这通常与未连接 IB 网点有关,但一切似乎都很好。
  • 也许你在代码中删除或重命名了 IBOutlet 或 IBAction,但 xib 文件仍然存在。
  • 我似乎无法在情节提要中将 MenuViewController 设置为我的 tableview 的类。这是因为该类在代码中缺少 UITableViewController 吗?
  • 我稍微更改了代码,现在可以构建它,但是现在当我点击菜单中的一个项目时,我在这一行出现错误 webView.loadRequest(NSURLRequest(URL : url as!NSURL)) 错误是Could not cast value of type 'Swift._NSContiguousString' (0x104848308) to 'NSURL' (0x102022e98).
  • 啊,因为 url 是 String 类型,你应该传递一个 NSURL,或者使用 webView.loadRequest(NSURLRequest(URL: NSURL(string: url as!String)!) 将 String 转换为 NSURL )
【解决方案2】:

你可以用notifications解决这个问题

首先,您需要为显示菜单项的 tableView 创建一个UITableViewController。 (如果您还没有这个以编程方式设置菜单项;我建议以编程方式执行此操作)

\[tableView(_:didSelectRowAtIndexPath:)\] 方法中,您需要发布一个通知,其中包含已选择项目的信息。

例子:

let kMenuItemSelectedNotification = "MenuItemSelected" //this should be a global constant. It identifies the notification

let infoDictionary = ["menuItem":"item1"] //a dictionary containing all information you need in your VC to change the WebView

NSNotificationCenter.defaultCenter().postNotificationName(kMenuItemSelectedNotification, object:nil, userInfo:infoDictionary)

在包含WKWebView 的视图控制器中,您需要为viewDidLoad 中的通知添加观察者。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "selector for your method:", name: kMenuItemSelectedNotification, object: nil)

您使用选择器调用的方法处理 WebView 内容的更改。

如果你还不熟悉通知,你应该看看this 教程。它解释了如何调用和接收通知以及处理在userInfo中发送的信息

【讨论】:

  • 谢谢。我已经在情节提要中创建了 tableview,因此我将研究以编程方式创建它。通知教程也很有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 2012-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多