2016-05-18 64 views
0

我想這對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) 
    } 

,但它不工作的打印(「卓悅」)也沒有顯示。 當我點擊changeMapType按鈕時,它只是做任何事情... 你有什麼想法嗎? :/

+1

什麼是你重新使用目的的委託方法?屏幕如何顯示?它應該實際上是一個子視圖控制器擁有的部分屏幕? – Wain

+0

我有地圖的第一個屏幕,我有其他的屏幕(選項屏幕),我想改變地圖類型,所以我有2個視圖控制器第一個顯示地圖和第二個選項根據這張地圖和其他像斷開連接等。 –

+0

最好是有兩個獨立的MKMapView實例,只需傳遞任何變量(或多個),讓它們通過委託或設置作爲屬性看起來相似(例如地圖中心,當前地圖類型等) ) – shim

回答

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