简体   繁体   English

再次调用viewdidload并再次显示viewdid

[英]Calling viewdidload and viewdidappear again

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. 我已经实现了一旦某些用户按下“不允许位置服务”按钮以再次引导他们进行设置以打开服务,便会弹出UIAlertview。

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 . 我面临的问题是,当用户最终打开按钮时,由于我的数据调用函数位于viewdidloadviewdidappear ,因此数据没有加载到tableview中。 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. 当我这样做时,它在使应用程序崩溃之前一直调用viewdidload大约百万次。 Any advices are appreciated 任何建议表示赞赏

Never never never never call viewDidLoad or viewDidAppear (except, in the latter case, to call super ). 永远永远永远永远不会调用viewDidLoadviewDidAppear (除了在后一种情况下,调用super )。 They are messages sent by the runtime to you, to report stages in the life of the view controller. 它们是运行时发送给您的消息,用于报告视图控制器生命周期的阶段。

Simply move your data calling functions outside of viewDidLoad and viewDidAppear : 只需将数据调用函数移到viewDidLoadviewDidAppear

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM