当我从 tableView 中选择一个单元格时,如何使用 navigationController 显示 WebView? [英] How to use navigationController to show up a WebView when I choose one cell from the tableView?
问题描述
当我们从tableView
中选择一个cell时,我们使用didSelectRowAtIndexPath
方法来实现具体的操作.
When we choose one cell from the tableView
, we use the didSelectRowAtIndexPath
method to implement the specific operation.
那么如何通过navigationController
跳转到webView
这样的另一个视图?
Then how to jump to another view like webView
via the navigationController
?
我想使用 prepareForSegue
来处理这个问题,如下所示,我只知道如何将数据从一个 viewController
传递到另一个 viewController
>.
I want to use the prepareForSegue
to handle this issue like below, and I just know how to pass data from one viewController
to another viewController
.
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!){
var channelC:ChannelController=segue.destinationViewController as ChannelController
channelC.delegate = self
//将channelData传递给ChannelController中去。
channelC.channelData=self.channelData
}
当我想显示另一个像 WebView
这样的视图时,我不知道如何在 didSelectRowAtIndexPath
方法中编码?
I don't know how to code in the didSelectRowAtIndexPath
method when I want to show up another view like WebView
?
我只是使用故事板来处理 viewController
开关的事情.谢谢
I just use the storyboard to handle viewController
switch thing.
Thanks
推荐答案
这里我为你创建一个简单的例子:
Here I create a simple example for you :
import UIKit
class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
var arr: [String] = ["google", "yahoo", "Swift"]
var index : Int = Int()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return arr.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
var identifier : NSString = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? UITableViewCell
if !(cell != nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: identifier)
}
cell?.textLabel.text = self.arr[indexPath.row]
return cell!
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
//store your clicked row into index
index = indexPath.row
// get to the next screen
self.performSegueWithIdentifier("goNext", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "goNext") {
var webViewController = segue.destinationViewController as googleViewController
//switch case for row which you have clicked
switch index{
case 0:
webViewController.url = "https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8"
case 1:
webViewController.url = "https://in.yahoo.com/"
case 2:
webViewController.url = "https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_456"
default:
println("nothing")
}
}
}
}
这是您的 googleViewController.swift
@IBOutlet weak var webView: UIWebView!
var url : String = String()
override func viewDidLoad() {
super.viewDidLoad()
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!)
webView.loadRequest(request)
}
也许这会对你有所帮助.
May be this will help you.
这篇关于当我从 tableView 中选择一个单元格时,如何使用 navigationController 显示 WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!