我是iOS上的新手。尝试为我的UIWebView
实现导航栏。
当用户单击任何行时,我的TableView
调用 Web 视图。我根据用户单击的索引加载不同的本地HTML文件。我的 Web 视图加载正确,但问题是,我想在那里保留一个导航栏和< back
按钮,以便用户可以单击它返回表视图。
这是我的ProductTableViewController
:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ProductTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ProductTableViewCell
let product = products[indexPath.row]
cell.name.text = product.name
cell.generic.text = product.generic
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("webView") as? WebViewController {
presentViewController(resultController, animated: true, completion: nil)
}
//print(indexPath.row)
clickedOn = indexPath.row
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let svc = segue.destinationViewController as! WebViewController;
//if clickedOn == 3{
svc.fileName = "Ace-VetBolus"
//}
}
而我的WebViewController
:
override func viewDidLoad() {
//print("got: "+self.fileName)
let url = NSBundle.mainBundle().URLForResource(self.fileName, withExtension:"html")
let request = NSURLRequest(URL: url!)
web1.loadRequest(request)
}
请帮我为此编写Swift
代码。
如果您要在ProductTableViewController
上呈现WebViewController
,并希望在导航栏上设置栏按钮,我建议您在 IB 的UINavigationController
中嵌入WebViewController
。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let navc = segue.destinationViewController as! UINavigationController;
let svc = navc.topViewController as! WebViewController;
//if clickedOn == 3{
svc.fileName = "Ace-VetBolus"
//}
}
将是prepareForSegue
方法的微小变化。
栏上的后退按钮必须手动添加。
didSelectRowAtIndexPath
替换为以下,它对我有用。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("webView") as? WebViewController {
let navController : UINavigationController = UINavigationController (rootViewController: resultController)
self.navigationController?.presentViewController(navController, animated: true, completion: nil)
}
//print(indexPath.row)
clickedOn = indexPath.row
}