0
私はProject 4でHacking With Swiftからボーナスチャレンジをしています。テーブルビューから別のWebサイトを選択する方法がわかりません。私は、Webビューで詳細ビューに移動するテーブルビューを持っていますが、ここからどこに行くのかはわかりません。テーブルから別のウェブサイトを選択するにはどうすればよいですか?
私のビューコントローラ
import UIKit
class ViewController: UITableViewController {
var websites = ["apple.com", "hackingwithswift.com"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//THIS DISPLAYS THE WEBSITE NAMES IN THE TABLE ROWS
return websites.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Website", for: indexPath)
//THIS DISPLAYS A WEBSITE NAME IN EACH CELL
cell.textLabel?.text = websites[indexPath.row]
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
vc.selectedWebsite = websites[indexPath.row]
//THIS PIECE BELOW LOADS ANIMATES THE NEW VIEW CONTROLLER
navigationController?.pushViewController(vc, animated: true)
}
}
}
と私の詳細ビューコントローラ任意のアイデアを事前に
import UIKit
import WebKit
class DetailViewController: UIViewController, WKNavigationDelegate {
var selectedWebsite: String?
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
let url = URL(string: "https://www.hackingwithswift.com")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
感謝。私はこれには新しく、何をすべきかについて断然感じています。
を選択したURLを渡しますか? – Tom
nevermind私はそれが働いている...ありがとう – Tom