Swift – 推送ViewController进入黑屏

所以我有一个包含tableView的View Controller.只要单击一个tableview单元格,就会推入另一个视图控制器.这个新的视图控制器有自己的xib.文件swift文件.我在新的viewcontroller中有一个mapview,但是当推入时新的视图控制器是黑色的.我该如何解决这个问题?
TableView didSelectRowAtIndexPath方法

func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var cell = tableView.cellForRowAtIndexPath(indexPath) as BusinessTableViewCell
    var business = self.results[indexPath.row]
    var mapView = BusinessMapViewController()
    mapView.business = business
    mapView.mapView = MKMapView()
    self.navigationController?.pushViewController(mapView,animated: true)
    println("\(business.name) SELECTED")
}

新视图控制器:

import UIKit
import MapKit
class BusinessMapViewController: UIViewController {

@IBOutlet weak var mapView: MKMapView!
var business:Business!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    let location = CLLocationCoordinate2D(
        latitude: 37.774929,longitude: -122.419416
    )
    let span = MKCoordinateSpanMake(0.1,0.1)
    let region = MKCoordinateRegion(center: location,span: span)
    println(business.latitude)
    mapView.setRegion(region,animated: true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application,you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

解决方法

如果您的新ViewController有自己的swift文件,那么您可以在didSelectRowAtIndexPath方法中尝试这样的事情:

let mapView = self.storyboard?.instantiateViewControllerWithIdentifier("MapViewController") as BusinessMapViewController

之后你可以用这个推送到nextViewController:

self.navigationController?.pushViewController(mapView,animated: true)

但不要忘记添加StoryBoard ID,否则会崩溃.

你的代码将是:

func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath) as BusinessTableViewCell
var business = self.results[indexPath.row]
let mapView = self.storyboard?.instantiateViewControllerWithIdentifier("MapViewController") as BusinessMapViewController
mapView.business = business
mapView.mapView = MKMapView()
self.navigationController?.pushViewController(mapView,animated: true)
println("\(business.name) SELECTED")
}

可能这会对你有所帮助.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...