无法快速显示第一个视图控制器将值传递给第二个视图控制器
Posted
技术标签:
【中文标题】无法快速显示第一个视图控制器将值传递给第二个视图控制器【英文标题】:Unable to show first viewcontroller passed values to second viewcontroller in swift 【发布时间】:2020-08-11 10:19:51 【问题描述】:我的第二个 viewcontroller 包含 mkmapview.. 所有时间地图必须仅在文本字段中显示当前位置地址值,但是当我从 firstviewcontroller 发送值时,只有它应该在其文本字段中显示 firstviewcontroller 的地址值
最初显示当前位置值的第二个视图控制器文本字段(如:城市、街道等)
现在我将第一个 viewcontroller 地址发送到 secondviewcontroller.. 但是这里 secondviewcontroller 总是只显示当前位置值.. 为什么?
firstvc 代码:向 2ndvc 发送地址:
func btnEditTapped(cell: EditAddressTableViewCell)
if let indexPath = self.addeditTableview.indexPath(for: cell)
print("edit button address index \(indexPath.row)")
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "ProfileAddressViewController") as! ProfileAddressViewController
var addrLo = addressArray[indexPath.row]
print("in edit total mode address \(addrLo.pincode)")// in console in edit total mode address Optional("530013")
viewController.editPincode = addrLo.pincode
viewController.editStree = addrLo.streetName
viewController.editColony = addrLo.city
self.navigationController?.pushViewController(viewController, animated: true)
我的 secondvc 代码:这里我无法显示 firstvc 的值,为什么?
class ProfileAddressViewController: UIViewController, CLLocationManagerDelegate, UISearchBarDelegate, DataEnteredDelegate
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D()
let locationManager = CLLocationManager()
var addressModel : ProfileModelUserAddress?
var editAddArray = [String]()
var addressArray = [String]()
@IBOutlet weak var titleNameLabel: UILabel!
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var pincodeField: UITextField!
@IBOutlet weak var cityField: UITextField!
@IBOutlet weak var streetField: UITextField!
@IBOutlet weak var mapView: MKMapView!
var editPincode: String?
var editStree: String?
var editColony: String?
var isEdit = Bool()
override func viewDidLoad()
super.viewDidLoad()
self.locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
if isEdit
titleNameLabel.text = "Edit Address"
pincodeField.text = editPincode
streetField.text = editStree
colonyField.text = editColony
@IBAction func backBtnClicked(_ sender: Any)
self.navigationController?.popViewController(animated: true)
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
guard let _: CLLocationCoordinate2D = manager.location?.coordinate else return
let userLocation :CLLocation = locations.last! as CLLocation
latitude = userLocation.coordinate.latitude
logitude = userLocation.coordinate.longitude
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(userLocation) (placemarks, error) in
if (error != nil)
print("error in reverseGeocode")
let placemark = placemarks! as [CLPlacemark]
if placemark.count>0
let placemark = placemarks![0]
print(placemark.locality!)
print(placemark.administrativeArea!)
print(placemark.country!)
let placemarkDictonary: NSDictionary=placemark.addressDictionary as! NSDictionary
self.pincodeField.text=placemarkDictonary["ZIP"] as? String
self.cityField.text=placemarkDictonary["City"] as? String
self.streetField.text=placemarkDictonary["Street"] as? String
let center = CLLocationCoordinate2D(latitude: latitude!, longitude: logitude!)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
mapView.setRegion(region, animated: true)
let myAnnotation: MKPointAnnotation = MKPointAnnotation()
myAnnotation.coordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
myAnnotation.title = "Current location"
mapView.addAnnotation(myAnnotation)
请告诉我,如何在第二个 vc 中显示 firstvc 传递的值
【问题讨论】:
将此行添加到您的 firstVC 'viewController. isEdit = true' @BijenderSinghShekhawat,如果我给 'viewController. isEdit = true' 标题正在改变,但 pincodeField、streetField、colonyField 没有改变……为什么 我不知道...您应该逐步调试以了解实际情况 @BijenderSinghShekhawat,谢谢你知道了 【参考方案1】:您是否在其他地方为isEdit
设置了值?如果不是,isEdit
的初始值将是 false
,因为您使用的是 Bool
的初始化程序,默认情况下返回 false
,因此,您的代码会为 if
内的文本字段初始化值语句永远不会运行。这就是为什么您看不到从第一个 VC 传递的值的原因。
【讨论】:
如果我给'viewController. isEdit = true'
标题正在改变但 pincodeField、streetField、colonyField 没有改变......为什么?以上是关于无法快速显示第一个视图控制器将值传递给第二个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
执行 segue 时将字符串传递给第二个视图控制器的 UILabel