Xcode 无法将数据和触发器函数从ViewController传递到由xib生成的自定义UIView
我对iOS编程非常陌生,所以请原谅我。我已经到处搜索,但我无法找到以下问题的解决方案。我有一个ViewController,在本例中是ThirdViewController,从这里我需要通过委托将坐标发送到名为MapWeatherView的自定义视图,但我似乎无法触发自定义视图中的函数来更新其在UI中的值。我知道我可以将这些值传递给Xcode 无法将数据和触发器函数从ViewController传递到由xib生成的自定义UIView,xcode,uiview,delegates,Xcode,Uiview,Delegates,我对iOS编程非常陌生,所以请原谅我。我已经到处搜索,但我无法找到以下问题的解决方案。我有一个ViewController,在本例中是ThirdViewController,从这里我需要通过委托将坐标发送到名为MapWeatherView的自定义视图,但我似乎无法触发自定义视图中的函数来更新其在UI中的值。我知道我可以将这些值传递给@ibvar-mapWeatherView:mapWeatherView,但我应该通过委托来完成这项工作。我应该在自定义视图中设置类似于delegate=self的内
@ibvar-mapWeatherView:mapWeatherView代码>,但我应该通过委托来完成这项工作。我应该在自定义视图中设置类似于delegate=self
的内容,但我不知道这是不是一种方式,我也无法这样做。以下是视图控制器和自定义视图的代码
第三个视图控制器类(我必须从这里发送坐标)
自定义视图(我需要使用上面第三个控件的坐标通过委托在这里触发函数)
此外,这是我故事板上的一个屏幕,可以更好地解释结构:
任何帮助都将不胜感激!请询问您是否需要任何澄清,并提前感谢您的帮助
import UIKit
import MapKit
import CoreLocation
protocol MapViewControllerDelegate {
func mapViewController(latitude: String, longitude: String)
}
class ThirdViewController: UIViewController, MKMapViewDelegate {
var currentPinLocation: CLLocationCoordinate2D? = nil
@IBOutlet weak var mMapView: MKMapView!
@IBOutlet weak var togglePinMode: UISwitch!
// IBOUTLET FOR THE CUSTOM VIEW
@IBOutlet weak var mapWeatherView: MapWeatherView!
fileprivate let locationManager: CLLocationManager = CLLocationManager()
var delegate: MapViewControllerDelegate?
// SETTING THE DELEGATE FOR THE CUSTOM VIEW??? THE ONE ABOVE IS FOR ANOTHER VIEW CONTROLLER
var mapWeatherDelegate: MapWeatherViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
togglePinMode.setOn(false, animated: false)
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.startUpdatingLocation()
mMapView.showsUserLocation = true
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(ThirdViewController.excecutedLongPress(_:)))
longPress.minimumPressDuration = 1.5
mMapView.addGestureRecognizer(longPress)
}
override func viewDidAppear(_ animated: Bool) {
updateWeatherView()
centerOnTarget(mMapView.userLocation.coordinate)
}
func centerOnTarget(_ target: CLLocationCoordinate2D) {
let center = CLLocationCoordinate2D(latitude: target.latitude, longitude: target.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.11, longitudeDelta: 0.11))
mMapView.setRegion(region, animated: false)
}
@objc func excecutedLongPress(_ recognizer: UIGestureRecognizer) {
if !togglePinMode.isOn {
return
}
let previousPin = mMapView.annotations.filter({!($0 is MKUserLocation)})
mMapView.removeAnnotations(previousPin)
let pressLocation = recognizer.location(in: self.mMapView)
let pressCoordinates : CLLocationCoordinate2D = mMapView.convert(pressLocation, toCoordinateFrom: self.mMapView)
let newPin = MKPointAnnotation()
newPin.coordinate = pressCoordinates
currentPinLocation = pressCoordinates
mMapView.addAnnotation(newPin)
// THIS SHOULD PASS DATA AND TRIGGER FUNCTION IN MapWeatherView (custom UIView)
mapWeatherDelegate?.updateMapWeatherView(coordinates: currentPinLocation!)
newPin.title = String(pressCoordinates.latitude) + "\n" + String(pressCoordinates.longitude)
}
func updateWeatherView() {
delegate?.mapViewController(latitude: String(mMapView.userLocation.coordinate.latitude), longitude: String(mMapView.userLocation.coordinate.longitude))
}
@IBAction func changeToPinMode(_ sender: UISwitch) {
if !togglePinMode.isOn {
centerOnTarget(mMapView.userLocation.coordinate)
mMapView.showsUserLocation = true
for annotation in mMapView.annotations {
mMapView.view(for: annotation)?.isHidden = true
}
} else {
if currentPinLocation == nil {
currentPinLocation = mMapView.userLocation.coordinate
}
mMapView.showsUserLocation = false
for annotation in mMapView.annotations {
mMapView.view(for: annotation)?.isHidden = false
}
centerOnTarget(currentPinLocation!)
}
}
}
import UIKit
import MapKit
// PROTOCOL FOR DELEGATE
protocol MapWeatherViewDelegate {
func updateMapWeatherView(coordinates: CLLocationCoordinate2D)
}
class MapWeatherView: UIView, MapWeatherViewDelegate {
// THIS IS THE FUNCTION THAT NEEDS TO BE TRIGGERED VIA DELEGATE
func updateMapWeatherView(coordinates: CLLocationCoordinate2D) {
print("Received latitude: \(coordinates.latitude) and longitude: \(coordinates.longitude) !!!")
}
var delegate: MapWeatherViewDelegate?
let nibName = "MapWeatherView"
var contentView: UIView?
@IBOutlet weak var mapLatLabel: UILabel!
@IBOutlet weak var mapLonLabel: UILabel!
@IBOutlet weak var mapWeatherIcon: UIImageView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
guard let view = loadViewFromNib() else { return }
view.frame = self.bounds
self.addSubview(view)
contentView = view
}
func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}
}