我正在使用UISplitViewController以所有可见模式显示,掌握和详细页面。只有Potrait方向。我在masterVC中使用可扩展表视图。所以我在章节中添加了一个视图。如果我点击该视图,URL应该反映在webView中,具体是VC。但是,对于我的代码,我收到一个错误。我没有使用任何导航控制器。直接我已将splitViewController连接到master和detail视图控制器。我在下面描述。请帮助我。
详情视图控制器
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
}
在主视图控制器中
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
}
在sectionHeaderTapped
中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??
答案 0 :(得分:1)
我创建了一个类似的应用,但我的代码没有问题。这是我的代码:
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