SetNeedsDisplay not working

I have seen many threads related to this problem, but no one is addressing my case (I think).

My case should be simple, I have a custom UIView in my controller, from my controller I use [ self.myView setNeedsDisplay ], and it works fine.

I am having problems when I try to call this from within the UIView itself ... I have a notification sent from another class and it is received by my view (this works) with the information it passes, I update the internal properties of this view, and I call [self setNeedsDisplay ], wanting to update my screen with new states, but nothing happens, I used the NSLOG inside my drawRec method, and it is not called at this time, it is called only when my controller class calls setNeedsDisplay , and when it happens update which should have happened earlier, displayed on the screen ... I do not know why it should not be updated to ...

Here is the code:

My controller is requesting an update: (works fine!)

 - (void)addNodeToNetwork:(DTINode *)node { [self.myNetwork addNodeInTheNetwork:node]; self.gridView.nodesToDraw = [self.myNetwork.nodesInNetwork copy]; CGRect tempRec = CGRectMake(node.nodePosition.x, node.nodePosition.y node.nodePosition.x, node.nodePosition.y); NSValue *rectObj = [NSValue valueWithCGRect:tempRec]; //transforma o cgrect num objeto [self.gridView.fatherNodes setValue:rectObj forKey:node.nodeName]; [self.gridView setNeedsDisplay]; } 

My notification method is trying to update my drawing: (Not working!)

 - (void) receiveTestNotification:(NSNotification *) notification { NSDictionary *userInfo = notification.userInfo; DTINode *notificationNode = [userInfo objectForKey:@"myNode"]; NSLog(@"Im Here!"); for (DTINode *node in self.nodesToDraw) { NSLog(@"Here too"); if(node.nodeName == notificationNode.fatherNode) { CGRect temp = CGRectMake(notificationNode.nodePosition.x, notificationNode.nodePosition.y, node.nodePosition.x, node.nodePosition.y); NSValue *tempObj = [NSValue valueWithCGRect:temp]; [self.fatherNodes setObject:tempObj forKey:notificationNode.nodeName]; [self setNeedsDisplay]; NSLog(@"Should REDRAW NOW!"); // It print this but no drawing is made! } } } 

I do not insert my drawRect here because it works, the problem is that it is not called from inside my UIView setNeedsDisplay !

Does anyone have an idea why this is not working?

+7
source share
2 answers

After much testing, I saw something related to threads, and the fact that setNeedsDisplay needs to be called only in mainThread ... besides, I never started a split thread in this class, the class that raised the notification was in the secondary thread. .. and obviously this was causing the problem ...

To solve it, I just set setNeedsDisplay to be called in the main thread.

 dispatch_async(dispatch_get_main_queue(), ^{ [self setNeedsDisplay]; }); 

Swift 3:

 DispatchQueue.main.async { [weak self] in self?.setNeedsDisplay() } 
+25
source

I think the correct way to use it is:

 [self setNeedsDisplay:YES]; 

Although I always have problems in order to work :(

+1
source

All Articles