Ios 调用viewdidload和ViewDid将再次出现
我的应用程序是基于地理位置的应用程序。我已经实现了在一些用户按下“不允许位置服务”按钮时立即弹出UIAlertview,引导他们再次进行设置以打开该服务 我面临的问题是,当用户最终打开按钮时,数据没有加载到tableview,因为我的数据调用函数位于Ios 调用viewdidload和ViewDid将再次出现,ios,swift,viewdidload,Ios,Swift,Viewdidload,我的应用程序是基于地理位置的应用程序。我已经实现了在一些用户按下“不允许位置服务”按钮时立即弹出UIAlertview,引导他们再次进行设置以打开该服务 我面临的问题是,当用户最终打开按钮时,数据没有加载到tableview,因为我的数据调用函数位于viewdidload和viewdidappear中。有没有办法再次调用这些函数 我做了如下类似的事情,它完全崩溃了我的应用程序: extension ExploreViewController: CLLocationManagerDelegate
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数百万次,直到它崩溃应用程序。感谢您的建议千万不要调用viewDidLoad
或viewDidAppear
(在后一种情况下,调用super
除外)。它们是运行时发送给您的消息,用于报告视图控制器生命周期中的各个阶段。决不调用viewDidLoad
或viewDidAppear
(在后一种情况下,调用super
)除外。它们是运行时发送给您的消息,用于报告视图控制器生命周期中的各个阶段
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
}
}只需将数据调用函数移到viewDidLoad
和viewDidAppear
之外即可:
而不是
override func viewDidLoad() {
// do some stuff
}
写
只需将数据调用函数移到viewdiload
和viewdilease
之外:
而不是
override func viewDidLoad() {
// do some stuff
}
写
那么,我该如何加载数据呢?我在视图中取出代码,并将其放入我的代码中。但是它没有运行,因为它在接收地理位置数据之前就已经运行了。我只是试着让ViewDid自己出现,它成功了。我还是不应该这样做吗?那么,如何加载数据呢?我在视图中取出代码,并将其放入我的代码中。但是它没有运行,因为它在接收地理位置数据之前就已经运行了。我只是试着让ViewDid自己出现,它成功了。我还是不应该这样做吗?