用不同的按钮更改 WKWebview URL ViewController
Change WKWebview URL with button in different ViewController
我是 iOS 开发的新手,想尝试使用 webview 构建混合 iOS 应用程序。我一直在学习,并且在这里或其他网站上找到了我需要的大部分答案,但我已经到了卡住的地步。
我在主 ViewController 中设置了 webview,所有加载都正常。我正在使用一个库来创建一个效果很好的侧边菜单。然而,由于图书馆的工作方式,菜单内置在另一个 ViewController 中。我想用将加载到 webview 中的 links 填充侧面菜单。这可能吗?
编辑:我可能还应该提到我在 swift.
中做这一切
EDIT2:这是我用于侧边菜单的框架的 link:https://github.com/jonkykong/SideMenu
该框架几乎允许您使用表视图构建自己的视图。
不知道我是否理解正确你的问题。以下是我对你的问题的看法:
你的 MenuViewController 包含一个 UITableView,它是一个菜单列表,当一个单元格被选中时,你的 MainViewController 的 webview 加载一个 link URL,但是这个 URL 放在 MenuViewController ,对吧?
如果这是您的问题,您可以按照以下解决方案解决它:
MainViewController 添加通知观察者,MenuViewController post 添加通知
// 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://whosebug.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)
}
}
你可以用 notifications
解决这个问题
首先,您需要为显示菜单项的 tableView 创建一个 UITableViewController
。 (如果您还没有这个以编程方式设置菜单项;我建议以编程方式执行此操作)
在 \[tableView(_:didSelectRowAtIndexPath:)\]
方法中,您需要 post 包含已选择项目的信息的通知。
示例:
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
中发送的信息
我是 iOS 开发的新手,想尝试使用 webview 构建混合 iOS 应用程序。我一直在学习,并且在这里或其他网站上找到了我需要的大部分答案,但我已经到了卡住的地步。
我在主 ViewController 中设置了 webview,所有加载都正常。我正在使用一个库来创建一个效果很好的侧边菜单。然而,由于图书馆的工作方式,菜单内置在另一个 ViewController 中。我想用将加载到 webview 中的 links 填充侧面菜单。这可能吗?
编辑:我可能还应该提到我在 swift.
中做这一切EDIT2:这是我用于侧边菜单的框架的 link:https://github.com/jonkykong/SideMenu
该框架几乎允许您使用表视图构建自己的视图。
不知道我是否理解正确你的问题。以下是我对你的问题的看法:
你的 MenuViewController 包含一个 UITableView,它是一个菜单列表,当一个单元格被选中时,你的 MainViewController 的 webview 加载一个 link URL,但是这个 URL 放在 MenuViewController ,对吧?
如果这是您的问题,您可以按照以下解决方案解决它:
MainViewController 添加通知观察者,MenuViewController post 添加通知
// 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://whosebug.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)
}
}
你可以用 notifications
解决这个问题首先,您需要为显示菜单项的 tableView 创建一个 UITableViewController
。 (如果您还没有这个以编程方式设置菜单项;我建议以编程方式执行此操作)
在 \[tableView(_:didSelectRowAtIndexPath:)\]
方法中,您需要 post 包含已选择项目的信息的通知。
示例:
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