从UITableView行选择导航到UIWebView

时间:2017-02-04 17:44:08

标签: ios swift uiwebview

以下是我的第一个视图控制器上的代码,其中包含tableview

我想在下一页展示uiwebview。

import UIKit

class menuTableViewController: UITableViewController {

var menuNameArr:Array = [String]()
var iconeImage:Array = [UIImage]()
var urlstring:Array = [URL]()

override func viewDidLoad() {
    super.viewDidLoad()

    menuNameArr = ["Anasayfa","Sınıf Listeleri","Ders Programı","Portal","Mesaj","Hesabım","İletişim","Paylaş","Hesaptan Çıkış"]

    iconeImage = [UIImage(named: "anasayfa")!,
                     UIImage(named: "sinif")!,
                      UIImage(named: "ders")!,
                    UIImage(named: "portal")!,
                     UIImage(named: "mesaj")!,
                   UIImage(named: "hesabim")!,
                  UIImage(named: "iletisim")!,
                    UIImage(named: "paylas")!,
                     UIImage(named: "hesap")!]

     urlstring = [URL(string:"http://sinifdefterim.com/portal.aspx")!,
            URL(string:"http://sinifdefterim.com/siniflistesi.aspx")!]
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "menuTableViewCell") as! menuTableViewCell

    cell.imgIcon.image = iconeImage[indexPath.row]
    cell.lblMenuName.text! = menuNameArr[indexPath.row]
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier:"ssss", sender: urlstring[indexPath.row])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let destinationNavigationController = segue.destination as! UINavigationController
    let mainVC:ViewController = destinationNavigationController.topViewController as! ViewController
    mainVC.mainURL = sender as! URL
}
}

如何在下一个viewcontroller上打开webview上的选定网址? 任何帮助表示赞赏

1 个答案:

答案 0 :(得分:1)

menuTableViewController 上,您在URL数组中只有2个元素。所以你必须在didSelect方法中写条件为:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

   if indexPath.row == 0 || indexPath.row == 1 {
        performSegue(withIdentifier:"ssss", sender: urlstring[indexPath.row])
   }
}

在你的下一个视图控制器上,它是UIViewController:

1)将 UIWebView 添加到View。使用助理编辑器连接 webview 出口并命名您的网络视图。

2) UIWebView viewDidLoad 中加载网址:

@IBOutlet weak var myWebView: UIWebView!

var mainURL = ""  //This value you are getting from menuTableViewController

override func viewDidLoad() {
super.viewDidLoad()

    let url = URL(string: mainURL)
    let requestObject = URLRequest(url: url!)
    myWebView.loadRequest(requestObject)
}