SetNeedsDisplay not working
Asked Answered
W

2

10

I saw many threads related to this issue, but none addresses my case (I think).

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

I'm having problems when Im trying to call this from inside of the UIView itself...I have a notification being sent from another class, and it is being received by my View (this works) with the information it passes, I update internal properties of this view and than I'm calling the [self setNeedsDisplay] wishing to have my screen updated with the new states, but nothing is happening, I used a NSLOG inside my drawRec method, and it is not being called at this time, it is only called when my controller class call the setNeedsDisplay, and when that happens, the update that should have happened before is showed on screen... I don't know why its not updating before...

Here are some code:

My controller asking for update: (works OK!)

- (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 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'm not pasting my drawRect here because it works, the problem is that is is not being called from inside my UIView setNeedsDisplay!

Anyone have any idea why this is not working????

Wendt answered 17/12, 2012 at 14:46 Comment(0)
W
28

After alot of testing, I saw something related to threads and the fact setNeedsDisplay should only be called in the mainThread...besides I never started a separeted thread in this classes, the class that raised the Notification was in a secondary thread...and aparently this was causing the issue...

to Solve it I just forced 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()
}
Wendt answered 20/12, 2012 at 13:5 Comment(1)
It does work in iOS 9.3.1 for me. The issue was I was on another thread just like the solution says.Tweeze
T
0

I think the correct way to use it is:

[self setNeedsDisplay:YES]; 

Although I always have problems to get that working :(

Tantalic answered 7/1, 2013 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.