How do I display characters to a UILabel as I loop through them?


I have a method that performs a loop on a separate thread. For reasons that work for my program I call this method using,

[self performSelectorInBackground:@selector(myMethod:) withObject:arg];

And the actual method,

- (void)myMethod:(NSString *)arg { NSAutoreleasePool *pool = [NSAutoreleasePool new]; for (int i = 0; i < [arg length]; i++) { unichar ch = [arg characterAtIndex:i]; NSLog(@"Processing character %c",ch); NSString *currentChar = [[NSString alloc] initWithFormat:@"%c", ch]; viewController.outputLabel.text = currentChar; [currentChar release]; switch (ch) { //Do my stuff } [pool release]; }

Now my problem is that ONLY THE LAST character that was processed gets displayed in my UILabel, yet when I check my console while running the program, as the characters are being processed they are being displayed to the console one-by-one (using NSLog). This is exactly the behavior I'd like to see going on in my label.

Also I should tell you that every time I'm processing a characters (in the switch statement) there is a least a slight delay because i'm calling [NSThread sleepForTimeInterval:delayTime]; at least a couple times.


You are attempting to update a view (your "outputLabel") from a thread other than the main thread, and this is not allowed.

You need to force the update to happen from the main thread. You can do this by calling

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

so, something like this:

[viewController.outputLabel performSelectorOnMainThread: @selector( setText: ) withObject: currentChar waitUntilDone: YES];


  • Objective C unicode character comparisons
  • How to split a string with the uppercase character in iPhone [closed]
  • addGlobalMonitorForEventsMatchingMask handler not getting triggered for certain inputs in Safari
  • Uppercase random characters in a NSString
  • AES 128 with CBC
  • How to convert NSString to ASCII value from String Value
  • Bind two JSF input fields with single property of backing bean
  • How to remove default command line arguments provided by Eclipse?
  • How do I use cURL & PHP to spoof the referrer?
  • Why won't my UITableViewCell deselect and update its text?
  • 403 forbidden error while sending messages to facebook connector through Unification Engine API
  • CFNetwork SSLHandshake failed (-9806) & (-9800) & (-9830)
  • Serverless Framework Dynamo DB Table Resource Definition with Sort Key
  • How to best manage SMTP clients
  • Unicorn and Rails eat up 2x MySQL connections
  • Python PIL to extract number from image
  • How Get arguments value using inline assembly in C without Glibc?
  • Optimizing the print function in Matlab
  • Security issues with PHP's Readfile method
  • Stop Bash Script if Hive Fails
  • Saving Changes After In-App Purchase Has Been Purchased
  • QLPreviewController hide print button in ios6
  • Python CGI os.system causing malformed header
  • DomPDF {PAGE_NUM} not on first page
  • AES padding and writing the ciphertext to a disk file
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • retrieve vertices with no linked edge in arangodb
  • trying to dynamically update Highchart column chart but series undefined
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal