如何解决无法将数据和触发函数从ViewController传递到由xib制成的自定义UIView
我在iOS编程方面还是一个新手,所以请您原谅。我在这里和那里进行了搜索,但无法找到以下解决方案。我有一个ViewController,在这种情况下为ThirdViewController,从这里我需要通过委托将坐标发送到名为MapWeatherView的自定义视图,但是我似乎无法触发自定义视图中的函数来更新UI中的值。我知道我可以将这些值传递给@IBOutlet weak var mapWeatherView: MapWeatherView!
,但是我应该通过委托来实现。我应该在自定义视图中设置类似delegate = self
的内容,但我不知道那样做,而且我也做不到。这是View Controller和自定义视图的代码。
第三视图控制器类(我必须从这里发送坐标)
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!)
}
}
}
自定义视图(我需要使用上面的ThirdViewController的坐标通过委托在这里触发函数)
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
}
}
这也是我的故事板中的一个屏幕,也许可以更好地说明结构:
任何帮助将不胜感激!请询问您是否需要任何澄清,并在此先感谢您的帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。