【问题标题】:Swift - Pushing ViewController in gives a black screenSwift - 推入 ViewController 会出现黑屏
【发布时间】:2023-03-28 20:07:01
【问题描述】:

所以我有一个包含 tableView 的视图控制器。每当单击 tableview 单元格时,就会推入另一个视图控制器。这个新的视图控制器有自己的 xib。文件和快速文件。我在新的视图控制器中有一个地图视图,但新的视图控制器在推入时是黑色的。我该如何解决这个问题? 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.
}
*/

}

【问题讨论】:

  • 你解决过这个问题吗?我面临着同样的问题;在我嵌入 navigationController 或将 mapView 推入之前,mapView 显示良好。

标签: swift uiviewcontroller


【解决方案1】:

如果您的新 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")
}

也许这会对你有所帮助。

【讨论】:

  • 但我的 BusinessMapViewController 也有自己的 xib。所以 self.storyboard 不工作吗?我会做 BusinessMapViewController.storyboard 还是什么?我什至不能在 xib 文件中使用故事板 ID,所以我会使用恢复 ID 吗?
  • @Dharmesh Kheni,为什么它不适用于直接实例化而适用于情节提要实例化?能给个线索或参考吗?谢谢
猜你喜欢
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多