[英]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.