swift UIActivityIndicatorView .hidden = false not working
Asked Answered
H

2

6

I am trying to show an Activity Indicator View once a button is pressed, but it does not want to show. If I don't set it to hidden at all, it shows, and when I try hide it, it hides. But if it is hidden, it will not show again.

Here is the code:

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    loading.hidden = true
}

@IBAction func submit() {
    loading.hidden = false
    loading.startAnimating()
    if chosenCategory == "" || txtName.text == "" || txtEmail.text == ""    {
        loading.stopAnimating()
        loading.hidden = true
    } else {
        println("animation")

No matter what, the stop animation works, and it can hide, but no matter what I do, it just seems to skip the loading.hidden = false and goes straight to printing the message out.

What could be happening?

Heterogynous answered 9/9, 2014 at 12:52 Comment(7)
There is no waiting here - every line that is in the same function, unless you're using something like a sleep() or some kind of asynchronous call, will be executed immediately one after the other. It's not like loading.startAnimating() will be called, things will go on hold, and then a while later the rest will happen. They will happen almost instantaneously one after the other. Show us more code for context.Pederasty
No, there is waiting, the ... is where other code goes. It calls to a server, and there is a waiting period (the request). And then I have even tried them in different methodsHeterogynous
Show us the rest of the code - can't help you otherwise.Pederasty
There is alot of code, over 200 lines. But I will try post what is relevant...Heterogynous
No offense but you seem to have a history of posting questions that don't get answers :) You should consider how you're asking/what code you're providing more generally on SO.Pederasty
They are legit questions with proper issues though that I cannot figure out. It's not my fault other people don't want to help.Heterogynous
When submit is hit, if the if statement is true, you will immediately hide loading. And people do want to help, but you need to provide enough information.Pederasty
P
18

I would recommend not using the .hidden property at all. Use the .hidesWhenStopped property instead and set it to true. Then simply invoke the .startAnimating() method and the UIActivityIndicatorView will automatically become visible and will be animating. Invoke the .stopAnimating() method and the UIActivityIndicatorView will automatically hide.

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    loading.hidesWhenStopped = true //this is all you need to change
}

@IBAction func submit() {
    loading.startAnimating() // becomes visible
    if chosenCategory == "" || txtName.text == "" || txtEmail.text == ""    {
        loading.stopAnimating() // goes into hiding
    } else {
        println("animation")
Prompter answered 26/6, 2016 at 8:53 Comment(2)
I thought toggling the hidden property is same as start/stopAnimating() if I set hidesWhenStopped to YES... but I am wrong. using start/stopAnimating() works great.Dyun
This is actually the correct answer, hiddenproperty is simply not working for UIActivityIndicatorView. I'm quite frustated that people answer questions here without even have tested problem at hand.Delibes
G
6

Use the GCD (Grand Central Dispatch) routines:

    self.activityIndicator.startAnimating()
    self.activityIndicator.hidden = false

    dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), { ()->() in
        ... // your long running code here

        self.activityIndicator.stopAnimating()
    })
Guarantor answered 9/9, 2014 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.