微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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")
}

可能这会对你有所帮助.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐