当前位置 : 主页 > 手机开发 > 其它 >

Swift – 推送ViewController进入黑屏

来源:互联网 收集:自由互联 发布时间:2021-06-11
所以我有一个包含tableView的View Controller.只要单击一个tableview单元格,就会推入另一个视图控制器.这个新的视图控制器有自己的xib.文件和 swift文件.我在新的viewcontroller中有一个mapview,但是
所以我有一个包含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")
}

可能这会对你有所帮助.

网友评论