加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift – 推送ViewController进入黑屏

发布时间:2020-12-14 04:36:44 所属栏目:百科 来源:网络整理
导读:所以我有一个包含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")
}

可能这会对你有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读