-1

My app is a geolocation based app. I've implemented to pop up a UIAlertview as soon as some users press "Don't allow location service" button to guide them to settings again to turn on the service.

The problem I'm facing is that when user finally turns on the button, the data are not being loaded to the tableview since my data calling functions are in viewdidload and viewdidappear. Is there a way to call those functions again?

I did something like below and it totally crashes my app:

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
}

When I was doing this way, it was kept calling viewdidload like million times before it crashed the app. Any advices are appreciated

vadian
  • 274,689
  • 30
  • 353
  • 361
Kahsn
  • 1,045
  • 3
  • 15
  • 25

3 Answers3

3

Never never never never call viewDidLoad or viewDidAppear (except, in the latter case, to call super). They are messages sent by the runtime to you, to report stages in the life of the view controller.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Then, how do I load the data then? I took out the code in viewdidappear in put it in my code. But then it wasn't running because it was running before it was receiving geolocation data. And I just tried just putting viewdidappear itself and it worked. Should I still not do this way? – Kahsn Nov 15 '15 at 17:32
3

Simply move your data calling functions outside of viewDidLoad and viewDidAppear:

Instead of

override func viewDidLoad() {
    // do some stuff
}

write

override func viewDidLoad() {
    super.viewDidLoad()
    doSomeStuff()
}

func doSomeStuff() {
    // do some stuff
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    // do your current logic
    doSomeStuff()
}
Community
  • 1
  • 1
luk2302
  • 55,258
  • 23
  • 97
  • 137
0
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

}

Tim
  • 2,089
  • 1
  • 12
  • 21