Reputation: 797
I'm trying to resize a UILabel to fit the text inside. Things appear to be very simple but here's what i'm doing:
[GKTurnBasedMatch loadMatchWithID:[matchReceived matchID] withCompletionHandler:^(GKTurnBasedMatch *updatedMatch, NSError *error){
NSPropertyListFormat plf;
NSMutableDictionary* gameData = [NSPropertyListSerialization propertyListWithData:updatedMatch.matchData options:NSPropertyListMutableContainersAndLeaves format:&plf error:nil];
myLabel.text = [gameData objectForKey:@"DictionaryText"];
[myLabel sizeToFit];
}
If i put the setting of the text add resizing to fit into a performSelector with a delay of some seconds, myLabel resizes as needed. But i need the label to update right after the game data is loaded.
I tried performing on the MainThread, but it didn't work.
Any ideas ?
Upvotes: 0
Views: 55
Reputation: 8207
All UI code must be run in main thread. So how to check, where code is run:
if ([NSThread isMainThread]) { /*main*/ } else { /*not main*/ }
How to force any code to run in main thread:
dispatch_async(dispatch_get_main_queue(), ^{ // serial, UI thread
....
});
Last thing is to make sure your variables can be modified inside block. Use __block
for local variables and use weak reference to self
with object properties:
__weak typeof(self) weakSelf = self;
__block BOOL success = NO;
[myOb doBlock:^(id item)
{
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf)
{
strongSelf.myLabel = @"changed";
success = YES;
}
}
Upvotes: 0
Reputation: 21883
First off Any code that interacts with the UI must be performed on the main thread. So it's very likely that a part of your problem is coming from not being on the main thread. I've seen all sorts of strange behaviour caused by UI code on background threads.
Once you have it on the main thread, you can start to diagnose the real problem. It's hard to say what the problem is, but one thing that comes to mind is that this could be a constraints related problem.
Upvotes: 2