我的应用是基于地理定位的应用。我已经实现了在一些用户按“不允许位置服务”按钮再次引导他们进行设置以打开服务时弹出UIAlertview。
我面临的问题是,当用户最终打开按钮时,数据不会被加载到tableview,因为我的数据调用函数位于viewdidload
和viewdidappear
。有没有办法再次调用这些函数?
我做了类似下面的事情,它完全崩溃了我的应用程序:
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几百万次。任何建议表示赞赏
答案 0 :(得分:3)
永远不要永远从不致电viewDidLoad
或viewDidAppear
(在后一种情况下,除了致电super
)。它们是运行时发送给您的消息,用于报告视图控制器生命周期中的各个阶段。
答案 1 :(得分:2)
只需将您的数据通话功能移到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()
}
答案 2 :(得分: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
}