swiftmkmapviewpushviewcontroller

Unable to show first viewcontroller passed values to second viewcontroller in swift


My second viewcontroller contains mkmapview.. all the time map must to show current location address values in textfields only but when i send values from firstviewcontroller then only it should show firstviewcontroller's address values in its textfields

Initially second viewcontroller textfields showing current location values(like: city, street, etc)

now i am sending first viewcontroller address to secondviewcontroller.. but here secondviewcontroller always showing current location values only.. why?

firstvc code: sending address to 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)
    }
}

my secondvc code: here i am unable to show firstvc's values why?

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

please let me know, how to show firstvc's passed values in 2nd vc


Solution

  • Do you set value for the isEdit somewhere else?. If not, the initial value for the isEdit will be false since you are using the initializer of Bool which returns false by default, therefore, your code that initiates values for your text fields inside the if statement will never get run. That's why you don't see the values you've passed from the first VC.