1

This is a very simple situation but I cannot seem to find the answer anywhere.

I'm building a Mac app in swift. So far I have a blank storyboard with one NSButton and one determinate NSProgressIndicator

The progress indicator was control+dragged into the Swift file for the ViewController to create an outlet called ProgressBar

The button was control+dragged into the Swift file for the ViewController so that it calls the function ButtonFunction(sender: NSButton) every time the button is clicked.

Inside ButtonFunction, I execute some code, call some other functions, and do a bunch of different unrelated actions. And after each action inside ButtonFunction, I have ProgressBar.increment(by:20)

The goal is basically just to increment ProgressBar a little bit more every time ButtonFunction executes the next line of code.

But the problem is, the progress indicator on the storyboard stays blank and only shows full blue / 100% at the very end when ButtonFunction has gone through all its lines of code and returned.

All of the ProgressBar.increment(by:20) statements appear to do absolutely nothing until the very end.

How can I repeatedly update the view of my application to show the advancing progress bar while executing the code inside of ButtonFunction?

I already tried calling loadView() inside ButtonFunction but it has no effect on the state of the progress indicator until the function is done or returns a value. I would like to update the view over and over again while inside of functions.

infinitedev108
  • 21
  • 1
  • 1
  • 3
  • 1
    Do you think their problem is related? https://stackoverflow.com/questions/38212874/swift-how-to-use-nsprogressindicator – Noah Nuebling Mar 19 '21 at 00:29
  • It's somewhat related, but it does not apply specifically for incrementing NSProgressIndicator inside of a function. Also, when I added the line `self.window.contentView?.addSubview(progressbar)` it says `Value of type 'AppDelegate' has no member 'window'` so I cannot seem to apply that solution. – infinitedev108 Mar 19 '21 at 03:53
  • 1
    The top answer says “When you set the value of the progress indicator, the OS X display mechanism won't actually draw the difference until the next time through the event loop, which doesn't happen until after your method returns. In other words, you're setting it all the way to 10 before the progress indicator even gets a chance to redraw itself, so all you see is the final filled state.” are you absolutely sure you won’t be able to apply that solution? It sounds super similar to your problem to me, but I only skimmed over it tbf, and I’ve never implemented a progress indicator either. – Noah Nuebling Mar 19 '21 at 14:22
  • You can control-drag from your window in interface builder to your AppDelegate code to create an outlet for your window. Then, you can access the window from your AppDelegate code through “self.window“ (if you named the outlet “window”) – Noah Nuebling Mar 19 '21 at 14:27

0 Answers0