无法快速显示第一个ViewController传递给第二个ViewController的值

问题描述

我的第二个viewcontroller包含mkmapview ..所有时间映射必须仅在文本字段中显示当前位置地址值,但是当我从firstviewcontroller发送值时,则只有它应该在其文本字段中显示firstviewcontroller的地址值

最初显示当前位置值(例如城市,街道等)的第二个Viewcontroller文本字段

现在我正在将第一个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传递的值

解决方法

您是否在其他地方设置了isEdit的值?否则,isEdit的初始值为false,因为您使用的是Bool的初始化程序,默认情况下返回false的初始化程序,因此,您的代码会为您在if语句中的文本字段将永远不会运行。这就是为什么您看不到第一个VC传递的值的原因。