[英]Calling viewdidload and viewdidappear again
我的應用程序是基於地理位置的應用程序。 我已經實現了一旦某些用戶按下“不允許位置服務”按鈕以再次引導他們進行設置以打開服務,便會彈出UIAlertview。
我面臨的問題是,當用戶最終打開按鈕時,由於我的數據調用函數位於viewdidload
和viewdidappear
,因此數據沒有加載到tableview中。 有沒有辦法再次調用這些函數?
我做了下面的事情,它完全崩潰了我的應用程序:
extension ExploreViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .Denied:
// Changed status to denied
self.locationAlert = UIAlertView(title: "Location Services Permission Needed", message: "Location service needs to be turned on to use Peek! Please press setting button below and turn the service on!", delegate: self, cancelButtonTitle: "Settings")
locationAlert.show()
break
case .AuthorizedWhenInUse:
self.viewDidLoad()
self.viewDidAppear(true)
break
default
break
}
當我這樣做時,它在使應用程序崩潰之前一直調用viewdidload大約百萬次。 任何建議表示贊賞
永遠永遠永遠永遠不會調用viewDidLoad
或viewDidAppear
(除了在后一種情況下,調用super
)。 它們是運行時發送給您的消息,用於報告視圖控制器生命周期的階段。
只需將數據調用函數移到viewDidLoad
和viewDidAppear
:
代替
override func viewDidLoad() {
// do some stuff
}
寫
override func viewDidLoad() {
super.viewDidLoad()
doSomeStuff()
}
func doSomeStuff() {
// do some stuff
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
// do your current logic
doSomeStuff()
}
func myCodeToRun() {
//put all the code you want to run here
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
myCodeToRun()
}
extension ExploreViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .Denied:
// Changed status to denied
self.locationAlert = UIAlertView(title: "Location Services Permission Needed", message: "Location service needs to be turned on to use Peek! Please press setting button below and turn the service on!", delegate: self, cancelButtonTitle: "Settings")
locationAlert.show()
break
case .AuthorizedWhenInUse:
myCodeToRun()
break
default
break
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.