Luc*_*cas 1 ios uistoryboardsegue swift ios8
我正在尝试将Objective-C方法转换为Swift.
Objective-C方法如下:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
[[segue destinationViewController] setUrl:string];
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我遇到的Swift方法(有问题)如下:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if (segue.identifier == "showDetail") {
var indexPath:NSIndexPath = self.tableView.indexPathForSelectedRow()
var string:NSString = self.feeds[indexPath.row] as String
string = string.stringByReplacingOccurrencesOfString("\n", withString: "")
segue.destinationViewController.setURL(string, forKey: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
主要问题是最后一行:
segue.destinationViewController.setURL(string, forKey: nil)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
"无法将表达式的类型'Void'转换为'NSURL!'"
这是我试图调用的控制器:
import UIKit
class DetailViewController: UIViewController
{
var url:NSString!
var webView:UIWebView!
override func viewDidLoad()
{
super.viewDidLoad()
var url:NSURL = NSURL(string:self.url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding))
var request:NSURLRequest = NSURLRequest(URL: url)
webView.loadRequest(request)
}
}
Run Code Online (Sandbox Code Playgroud)
我希望有人可以帮助我解决这个问题,我已经有一段时间在努力解决这个问题了.:/
谢谢!
在Objective C或Swift中访问destinationViewController时使用的正确模式是将其强制转换为正确的子类类型.这将允许您访问子类方法和属性.
所以你应该说
let myDestVC = segue.destinationViewController as MyViewControllerClass
Run Code Online (Sandbox Code Playgroud)
或者在目标C中
MyViewControllerClass *myDestVC =(MyViewControllerClass *)segue.destinationViewController;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5275 次 |
| 最近记录: |