Swift:触发TableViewCell以在另一个ViewController中的UIWebView中引导链接 [英] Swift: Triggering TableViewCell to lead to a link in a UIWebView in another ViewController
问题描述
当我点击 tableViewCell
时,我想要一个链接(特定于该单元格的 indexPath.row
)在一个带有webView的新viewController中打开。
When i tap a tableViewCell
I want a link (that is specific to that cell's indexPath.row
) to open up in a new viewController with a webView.
示例:我点击 tableView
中的第3个单元格,并在新的viewController上打开www.apple.com 。
Example: I tap the 3rd cell in the tableView
and www.apple.com opens up on a new viewController.
我试图用 didSelectRowAtIndexPath
来做到这一点,但我无法弄清楚如何放置代码一起,当我点击 tableViewCell
时,来自 linksArray
(我存储了所有链接)的代码一起工作
I'm trying to do that with didSelectRowAtIndexPath
but I can't figure out how to put the code together so that the code from the linksArray
(where i have all the links stored) works together when I tap the tableViewCell
我正在从Parse.com查询这些链接。这是我的数组:
I'm querying these links from Parse.com. This is my array:
var linksArray = [NSURL](
)
我创建了一个名为 LinkViewViewController
的新类,其中我只将webView连接为 @IBOutlet弱变量webView:UIWebView!
而没有别的。我拿出了 viewDidLoad
方法,以便我可以从 FeedTableViewController控制
LinkViewViewController
code>包含单元格的类。
I created a new Class called LinkViewViewController
where I have just the webView connected as a @IBOutlet weak var webView: UIWebView!
and nothing else. I took out the viewDidLoad
method so that I can control the LinkViewViewController
from the FeedTableViewController
class that has the cells.
这是我下面的代码
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let myWebView = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! LinkViewViewController
var url = NSURL(string:"\(links[indexPath.row])")
var request = NSURLRequest(URL: url!)
myWebView.webView.loadRequest(links[indexPath.row] as! NSURLRequest)
}
更新
我也试图这样做,这可能更符合逻辑。我正在访问ProductInfo类中存储在Parse中的链接。 productName
数组表示单元格的组织顺序,以便在点击产品名称时可以访问链接。
I am also trying to do it this way, which maybe more logical. I am accessing the links that are stored in Parse in the ProductInfo class. The productName
array represents in what order the cells are organized in so that the the links can be accessed when tapping on the product name.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let myWebView = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! LinkViewViewController
//Assuming that myWebView is the right method/way to go about it.
var link = PFObject(className: "ProductInfo")
link["pLink"] = productName[indexPath.row]
/* Code missing: I'm accessing where the link is (the
indexPath.row) associated with the productName.
I don't know how to go about this now... How do i access the
webView in the other viewController where the corresponding
link appears?
*/
}
我也尝试过创建从单元格到webView的segue但是也没有... ...
I've also tried creating a segue from the cell to the webView but that didn't work either...
如何设置 didSelectRowAtIndexPath
以便触发链接出现在新视图中?
How can I set up the didSelectRowAtIndexPath
so that a link is triggered to appear in a new view?
想法:
一个问题? info.plist
中的NSTransportSecurity
Thought: NSTransportSecurity
in the info.plist
also an issue?
任何帮助意味着很多。
谢谢。
推荐答案
您不应该使用 dequeueReusableCellWithIdentifier
来创建下一个视图控制器。您应该使用故事板的 instantiateViewControllerWithIdentifier
来创建实例:在故事板中为viewController设置Storyboard ID
You should not be using dequeueReusableCellWithIdentifier
to create your next view controller. You should use the storyboard's instantiateViewControllerWithIdentifier
to create the instance: Set the "Storyboard ID" for the viewController in your storyboard
如果将其设置为WebViewController,则可以使用以下命令创建实例:
If you set it to, for example, "WebViewController" you can then create the instance with:
let myWebView = self.storyboard!.instantiateViewControllerWithIdentifier("WebViewController") as! LinkViewViewController
我的建议是给 LinkViewViewController
它自己的URL属性,
My recommendation would be to give the LinkViewViewController
its own URL property,
var url : NSURL?
并在创建实例后传递URL:
and to pass the URL after creating the instance:
myWebView.url = url
然后出现新的VC:
self.presentViewController(myWebView, animated: true, completion: nil)
在LinkViewViewController的 viewDidLoad
中,将URL加载到webView中:
In the viewDidLoad
of LinkViewViewController, load the URL into the webView:
let URLRequest = NSURLRequest(self.url)
self.webView.loadRequest(URLRequest)
(未经测试输入;请原谅选项中的任何拼写错误/错误等)。
(Typed without testing; please forgive any typos/errors with optionals etc).
作为最后一点,此方法使用 didSelectRow
方法创建视图控制器实例并传递URL。另一种选择是通过从原型单元格拖动到视图控制器来使用segue。然后,您需要在 prepareForSegue
方法中实现类似的代码来设置网址。
As a final note, this method uses the didSelectRow
method to create the view controller instance and pass the URL. Another option would be to use a segue by ctrl-dragging from the prototype cell to the view controller. You then need to implement similar code in prepareForSegue
method to set the url.
这篇关于Swift:触发TableViewCell以在另一个ViewController中的UIWebView中引导链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!