Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Pushing ViewController in gives a black screen

So I have one View Controller that holds a tableView. Whenever a tableview cell is clicked, another view controller is pushed in. This new view controller has its own xib. file and swift file. I have a mapview in the new viewcontroller, but the new view controller is black when pushed in. How do I fix this? TableView didSelectRowAtIndexPath method:

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

New View controller:

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.
}
*/

}

like image 533
dhruvm Avatar asked Mar 16 '23 23:03

dhruvm


1 Answers

If your new ViewController have its own swift file then you can try something like this in didSelectRowAtIndexPath method:

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

after that you can push to the nextViewController with this:

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

but don't forget to add StoryBoard ID otherwise it will crash.

enter image description here

And your code will be:

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

May be this will help you.

like image 77
Dharmesh Kheni Avatar answered Apr 29 '23 16:04

Dharmesh Kheni