在ViewControllers Swift 3之间传递一个Double

时间:2017-09-10 22:43:22

标签: ios swift

总计noob到Swift 3.我需要做的就是通过2个用户输入文本字段从一个ViewController传递一个double值到另一个。我已经尝试了很多解决方案并阅读了在ViewControllers之间传递数据时可以找到的所有内容。我得到了一个'致命的错误:在解开一个Optional值时意外地发现了nil'。我很难理解变量的包装和展开,我确信这很简单。

这是我的第一个ViewController:

import UIKit

var longitude: Double?
var latitude: Double?



class ViewController: UIViewController {

@IBOutlet var getLongitude: UITextField!
@IBOutlet var getLatitude: UITextField!

@IBOutlet var mapbutton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

        }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func MapBtn(_ sender: Any) {

    performSegue(withIdentifier: "segue", sender: self)
    longitude = Double(getLongitude.text!)!
    latitude = Double(getLatitude.text!)!

}

}

这是SecondViewController:

class SecondViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let camera = GMSCameraPosition.camera(withLatitude: latitude!, longitude: longitude!, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    view = mapView
    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: latitude!, longitude: longitude!)
    marker.map = mapView

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

3 个答案:

答案 0 :(得分:1)

避免全局可变状态。将数据从一个场景发送到另一个场景。

试试这个:

class ViewController: UIViewController {

  @IBOutlet weak var longitudeTextField: UITextField!
  @IBOutlet weak var latitudeTextField: UITextField!
  @IBOutlet weak var mapButton: UIButton!

  @IBAction func didTapToMapButton(_ sender: UIButton) {

    // I assume your storyboard' name is Main. If not, change it below accordingly
    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)

    // I assume your destination view controller' identifier and type is SecondViewController. If not, change it below accordingly.
    if let secondViewController = mainStoryboard.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController {

      if let longitude = Double(longitudeTextField.text),
        let latitude = Double(latitudeTextField.text) {

        secondViewController.latitude = latitude
        secondViewController.longitude = longitude
      }

      present(secondViewController, animated: true, completion: nil)
    }
  }
}

class SecondViewController: UIViewController {

  var latitude: Double?
  var longitude: Double?

  override func viewDidLoad() {
    super.viewDidLoad()

    if let latitude = latitude,
      let longitude = longitude {

      let camera = GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: 6.0)
      let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
      view = mapView

      // Creates a marker in the center of the map.
      let marker = GMSMarker()
      marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
      marker.map = mapView
    }
  }
}

答案 1 :(得分:0)

1,解缠。当你试图打开变量,而不是强行打开它时,如果它没有,它可以更好地测试,或者你可以使用它来安全地解开它。

第二,将变量从一个VC传递到另一个VC。如您所知,在iOS中,这些VC实例与任何其他变量一样。您需要做的就是尝试获取对该VC的引用,然后将Double分配给其可访问变量。看一下这个link

答案 2 :(得分:0)

您可以在两个控制器之间创建一个segue,并通过覆盖为segue准备来传递变量。

将该变量传递给新控制器,并将该信息加载到viewDidAppear或viewWillAppear上的插座。

self.prepare(for: "segueName", sender: self)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "segueName" {

        if let vc = segue.destination as? DestinationViewController {

            // Pass Data to New View Controller
        }
    }
}