我正在使用UISplitViewController在全可见模式下显示,主页面和详细信息页面。仅Potrait定位。我在masterVC中使用可扩展表视图。所以我在部分中添加了一个视图。如果我点击该视图,则URL应该反映在webView中,该视图在detailVC中。但是,对于我的代码,我收到一个错误。我没有为此使用任何导航控制器。我直接将splitViewController连接到主视图控制器和详细视图控制器。我在下面描述。请帮助我。

详细视图控制器

func webViewLoader()
    {
        println("Came Inside Web View")
        let url = "http://makeapppie.com/2014/10/28/swift-swift-using-uiwebviews-in-swift/"
        let requestURL = NSURL(string:url)
        let request = NSURLRequest(URL: requestURL!)
        webView.loadRequest(request) //FATAL ERROR WHILE UNWRAPPING
    }


在Master View Controller中

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerView = UIView(frame: CGRectMake(0, 0, sideTableView.frame.size.width, 49)) //ADDING VIEW TO SECTION - FOR EXPANDABLE TABLEVIEW
        var layerLine = CALayer()
        layerLine.frame = CGRect(x: 0, y: headerView.frame.size.height, width:  headerView.frame.size.width, height: 1)
        layerLine.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 1.0).CGColor
        headerView.backgroundColor = UIColor(red: 242/255, green: 242/255, blue: 242/255, alpha: 1.0)
        headerView.tag = section

        headerView.layer.addSublayer(layerLine) //ADDING BOTTOM BORDER TO CELL

        let headerString = UILabel(frame: CGRect(x: 10, y: 10, width: sideTableView.frame.size.width-10, height: 30)) as UILabel
        headerString.text = sideTblData.objectAtIndex(section) as! NSString as String
        headerString.font = headerString.font.fontWithSize(15)
        headerView .addSubview(headerString)

        let headerTapped = UITapGestureRecognizer (target: self, action: "sectionHeaderTapped:")
        headerView .addGestureRecognizer(headerTapped)
        return headerView
    }


在标题中

func sectionHeaderTapped(recognizer: UITapGestureRecognizer) {
        let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc : DetailVC = storyboard.instantiateViewControllerWithIdentifier("web-link") as! atlDetailVC
        vc.webViewLoader()
}


在viewDidLoad中

let url = "http://makeapppie.com/2014/10/28/swift-swift-using-uiwebviews-in-swift/"
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!)
webView.loadRequest(request) //It is working. But, in method call, why I am getting fatal error??

最佳答案

我创建了一个类似的应用程序,但是我的代码没有问题。这是我的代码:

class MasterViewController: UITableViewController{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell

        cell.textLabel?.text = "\(indexPath.row)"

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let dvc = self.splitViewController?.viewControllers.last as! DetailViewController

        dvc.webViewLoader()
    }
}

class DetailViewController: UIViewController {
    @IBOutlet weak var WebView: UIWebView!

    func webViewLoader() {
        println("Came Inside Web View")
        let url = "http://www.google.com"
        let requestURL = NSURL(string:url)
        let request = NSURLRequest(URL: requestURL!)
        WebView.loadRequest(request) //FATAL ERROR WHILE UNWRAPPING
    }
}


更新1

我已经上传了Project

关于swift - swift 在UIWebView中加载URL时发生致命错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31283588/

10-10 00:19