2017-02-04 42 views
0

以下是由具有的tableview在它導航到一個UIWebView從UITableView的行選擇

我想告訴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上打開選定的網址? 任何幫助讚賞

+0

到底哪裏出問題了,你應該解釋在身體您的問題,不只是發佈了一堆代碼 – Simon

回答

0

在您的menuTableViewController你只有2個元素在URL數組。所以,你必須在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的查看。連接webview outlet使用助手編輯器並命名您的webview。

2)UIWebView的負載URL中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) 
} 
相關問題