ios - Passing a Double between ViewControllers Swift 3 -
total noob here swift 3. need pass double value 1 viewcontroller via 2 user input text fields. i've tried numerous solutions , have read can find on passing data between viewcontrollers. 'fatal error: unexpectedly found nil while unwrapping optional value'. have real hard time understand wrapping , unwrapping of variables , i'm sure it's simple.
here first 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 resources can recreated. } @ibaction func mapbtn(_ sender: any) { performsegue(withidentifier: "segue", sender: self) longitude = double(getlongitude.text!)! latitude = double(getlatitude.text!)! } }
and 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 marker in center of map. let marker = gmsmarker() marker.position = cllocationcoordinate2d(latitude: latitude!, longitude: longitude!) marker.map = mapview } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. }
}
avoid global mutable state. send data 1 scene 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) { // assume storyboard' name main. if not, change below accordingly let mainstoryboard = uistoryboard(name: "main", bundle: nil) // assume destination view controller' identifier , type secondviewcontroller. if not, change 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 marker in center of map. let marker = gmsmarker() marker.position = cllocationcoordinate2d(latitude: latitude, longitude: longitude) marker.map = mapview } } }
Comments
Post a Comment