我对iOS开发非常陌生,想尝试使用webview构建混合iOS应用程序。随着时间的推移,我学到了很多东西,在这里或其他网站上找到了我需要的大多数答案,但我已经到了一个卡住的地方。
我在主ViewController中有我的webview设置,所有加载都很好。我正在使用一个库来创建一个侧面菜单,它非常有效。然而,由于库的工作方式,菜单是建立在另一个ViewController。我想填充侧菜单的链接,将在webview中加载。这可能吗?
编辑:我可能还应该提到我是用swift做的。
EDIT2:这是一个链接到框架我使用的侧菜单:https://github.com/jonkykong/SideMenu
框架允许你使用tableview创建自己的视图
我不知道我是否正确理解了你的问题。以下是我对你的问题的看法:
你的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)
}
}
你可以通过通知来解决这个问题
首先,您需要为显示菜单项的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内容的变化。
如果你还不熟悉通知,你应该看看这个教程。说明了如何调用和接收通知,以及如何处理userInfo