我在optionViewController上尝试了这个:
protocol OptionControllerDelegate: NSObjectProtocol {
func changeMapDisplayMode(controller: OptionController)
}
class OptionController: UIViewController {
weak var delegate: OptionControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func changeMapType(sender: AnyObject) {
delegate?.changeMapDisplayMode(self)
}
}
,这在traceViewController中:
class TraceController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, OptionControllerDelegate {
@IBOutlet weak var mapKit: MKMapView!
var locationManager: CLLocationManager!
var location: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
mapKit.setUserTrackingMode(.Follow, animated: true)
mapKit.delegate = self
let optionController = OptionController()
optionController.delegate = self
if (CLLocationManager.locationServicesEnabled()) {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
}
func changeMapDisplayMode(controller: OptionController) {
print("bonjour")
let actionSheet = UIAlertController(title: "Map Types", message: "Select map type:", preferredStyle: UIAlertControllerStyle.ActionSheet)
let normalMapTypeAction = UIAlertAction(title: "Normal", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
//self.viewMap.mapType = kGMSTypeNormal
self.mapKit.mapType = .Standard
}
let satelliteMapTypeAction = UIAlertAction(title: "Satellite", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
//self.viewMap.mapType = kGMSTypeTerrain
self.mapKit.mapType = .Satellite
}
let hybridMapTypeAction = UIAlertAction(title: "Hybrid", style: UIAlertActionStyle.Default) { (alertAction) -> Void in
self.mapKit.mapType = .Hybrid
}
let cancelAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel) { (alertAction) -> Void in
}
actionSheet.addAction(normalMapTypeAction)
actionSheet.addAction(satelliteMapTypeAction)
actionSheet.addAction(hybridMapTypeAction)
actionSheet.addAction(cancelAction)
presentViewController(actionSheet, animated: true, completion: nil)
}
但它不起作用(" bonjour")也没有显示。 当我点击changeMapType按钮时,它只是做任何事情...... 你有什么想法吗? :/
答案 0 :(得分:1)
在应用程序的不同位置共享视图实例不是一个好习惯。
这里有多种选择。
也许最好的方法是使用允许OptionController
向TraceController
发送邮件的代理。
将代理人添加到OptionController
:
protocol OptionControllerDelegate {
func changeMapDisplayMode(...)
}
class OptionController: UIViewController {
...
weak var delegate : OptionControllerDelegate
...
}
在TraceController
class TraceController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, OptionControllerDelegate {
...
optionController.delegate = self // Do this on the instance of OptionController
...
func changeMapDisplayMode(...) {
// Do stuff
}
...
}