在ViewController中,我啓動了Location.swift中的類Location
,以便使用它的方法。如何從swift委託類方法將數據傳回ViewController?
in ViewDidLoad
,我打電話給Location
的第一個方法,它建立了位置服務(權限等)。同樣在Location中,有兩個來自CLLocationManagerDelegate的委託方法。
視圖控制器看起來像(簡體!):
class ViewController: UIViewController {
var location = Location();
override func viewDidLoad() {
location.initLocation();
//when updated data from delegated method, i want to get data to here, in order to output it in UI labels,etc
}
}
Location.swift樣子(簡化!):
import CoreLocation
class Location: NSObject, CLLocationManagerDelegate{
func initLocation(){
........
}
@objc func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading){
//i want to send newHeading data to viewController here
}
}
我可以很容易地從initLocation()
找回數據,如我所說,並定義該方法我自己。但是如果我需要從locationManager
方法獲取數據,那麼我不會調用該數據呢?
請問如果您有任何問題。我希望我已經充分解釋了!
乾杯
你需要的newHeading值每次它更新或做你想做的只是能夠調用這個類來獲取newHeading最近的值? – KotaBear233
你應該使用...代表團!創建一個協議,例如'LocationDelegate',並通過你的'ViewController'實現它。 – Losiowaty
@ KotaBear233每當它更新:) –