简体   繁体   English

在ViewControllers Swift 3之间传递Double

[英]Passing a Double between ViewControllers Swift 3

Total noob here to Swift 3. All I need to do is pass a double value from one ViewController to another via the 2 user input text fields. 这里对Swift 3来说是新手。我要做的就是通过2个用户输入文本字段将一个Double值从一个ViewController传递给另一个。 I've tried numerous solutions and have read everything I can find on passing data between ViewControllers. 我尝试了许多解决方案,并阅读了在ViewController之间传递数据时可以找到的所有内容。 I get a 'fatal error: unexpectedly found nil while unwrapping an Optional value'. 我收到“致命错误:在展开可选值时意外发现nil”。 I have a real hard time understand the wrapping and unwrapping of the variables and I'm sure it's something simple. 我很难理解变量的包装和展开,并且我敢肯定这很简单。

Here is my first ViewController: 这是我的第一个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!)!

}

}

And this is the SecondViewController: 这是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.
}

} }

Avoid global mutable state. 避免全局可变状态。 Send data from one scene to another. 将数据从一个场景发送到另一个场景。

Try this: 尝试这个:

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

1st, Unwrapping. 1,展开。 when you try to unwrap the variable, rather forcely unwrap it, it's better test if it's nil, or you could use if let to safely unwrap it. 当您尝试解开变量,而不是强行解开变量时,最好测试它是否为nil,或者如果可以安全地解开变量,则可以使用。

2nd, pass variable from one VC to another. 第二,将变量从一个VC传递到另一个。 As you know, in iOS, those VCs instances are like any other variable. 如您所知,在iOS中,这些VC实例与其他任何变量一样。 All you need to do is trying to fetch a reference to that VC, then assign the Double to its accessible variable. 您需要做的就是尝试获取对该VC的引用,然后将Double分配给它的可访问变量。 Take a look at this link 看看这个链接

You could create a segue between the two controllers and pass the variable by overriding prepare for segue. 您可以在两个控制器之间创建序列,然后通过覆盖准备序列来传递变量。

Pass that variable to the new controller and load that information to its outlet on viewDidAppear or viewWillAppear. 将该变量传递给新控制器,并将该信息加载到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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM