I want to manipulate views while in a for loop. I manipulate a view in the for loop, then the operations for the view are done at once after the for loop has ended. I tried to use other threads like GCD, but I noticed that a view is in the main thread. The operations are back to the main thread and they are put off after the for loop finishes.
What I want to do is update UITextView’s text while in the for loop. If I can’t operate the for loop in another thread, how can I do that? Are there other ways to do that?
Solution 1: Use a timer
In order to progressively add text to a textview, you can use an NSTimer.
Requirements
in your interface – the following ivars or properties:
Assuming the string is created and the textview is set up, you can create a function to create the timer and kick it off:
This is a basic working implementation, and you can vary it to pass in the string as userInfo for the timer, if it is not present at the class level. Then you could access it in your
addTextToTextViewselector withsender.userInfo. You can also adjust the timer interval and how exactly the text is added. I used half a second and character by character concatenation as an example.Solution 2: Use a loop
Requirements