隐藏活动指示器不起作用

时间:2015-01-16 23:22:37

标签: objective-c swift

活动指示器启动,但在调用隐藏功能时不会停止。我已经尝试将隐藏功能放在不同的地方,但它仍然没有隐藏。

隐藏活动指示器:Q0ViewController().hideActivityIndicator(self.view)

我使用此处的swift实用程序功能: https://github.com/erangaeb/dev-notes/blob/master/swift/ViewControllerUtils.swift

开始活动指标

override func viewDidLoad() {
    super.viewDidLoad()
    Q0ViewController().showActivityIndicator(self.view)
    self.locationManager.delegate = self //location manager start
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
}

查询后隐藏活动指示符:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
        if (error != nil) {
            println("Error:" + error.localizedDescription)
            //return
        }
        if placemarks.count > 0 {
            let pm = placemarks[0] as CLPlacemark
            self.displayLocationInfo(pm)
            currentLoc = manager.location
            currentLocGeoPoint = PFGeoPoint(location:currentLoc)
            var query = PFQuery(className:"test10000")
            query.whereKey("RestaurantLoc", nearGeoPoint:currentLocGeoPoint, withinMiles:100) //filter by miles
            query.limit = 1000 //limit number of results
            query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]!, error: NSError!) -> Void in
                if objects != nil {
                    unfilteredRestaurantArray = objects
                    originalUnfilteredArray = objects
                    println(objects)
                } else {
                    println("error: \(error)")
                }
            Q0ViewController().hideActivityIndicator(self.view) //HIDE
            }
        } else {
            println("error: \(error)")
        }
    })
}

主队列不是问题,因为dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), { ()->() in无法解决问题。

2 个答案:

答案 0 :(得分:1)

看起来您正在创建" Q0ViewController"的新实例。每一次。

相反,我建议将初始实例保留为您班级的属性:

// As a variable on the class instance
let myViewController = Q0ViewController()

// Initially show the activity indicator
self.myViewController.showActivityIndicator(self.view)

// Hide the activity indicator
self.myViewController.hideActivityIndicator(self.view)

希望这有帮助!

答案 1 :(得分:0)

与约书亚建议的相似,只是替换了:

Q0ViewController().showActivityIndicator(self.view)
and
Q0ViewController().hideActivityIndicator(self.view)

要:

self.showActivityIndicator(self.view)
and
self.hideActivityIndicator(self.view)