drawRect not being called

Apprentice
Posts: 11
Joined: 2009.07
Post: #1
I have a Class View:UIView, inheriting from class Controller:NSObject. Class View is responsible for the view and the drawRect method is overridden there, to draw what I want.

Now I want to call setNeedsDisplay from class Controller, which doesn't know about View yet. So I have told it via @class View, that there is a class View, defined an IBOutlet of type View and made the connection in the Interface Builder.

There, I have an Object of Class Controller, and its Outlet referencing to View.
Still, when calling setNeedsDisplay on the outlet within the Controller class, drawRect is not called.

I can call drawRect from the View class without any problems wrong. I even checked the Outlet for being nil, and it also has its accessors synthesized.

Did I miss anything? Are the connections wrong?

Thanks in advance!
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2009.07
Post: #2
I've decided to add some Code, maybe it helps.

Code:
#import "GameField.h"


@interface GameView : UIView {
IBOutlet GameField *controller;
}

@end

@implementation GameView


-drawRect
{
NSLog(@"Please please update me from GameField");
}



@class GameView;

@interface GameField : NSObject {
int fieldSize;

IBOutlet GameView *viewController;
}

@end

@implementation GameField

-someSelector
{

//here I want the View to redraw itself
[viewController setNeedsDisplay];

}


=========
In Interface Builder I have connected the Outlets, one from GameField to GameView and the other vice versa.
Now the GameView accesses the data in GameField correctly, theres no problem. But I cant access the GameView from the GameField - drawRect is not called. Why not? I have done everything in the same way-.... one works, the other doesn't........
Quote this message in a reply
Member
Posts: 283
Joined: 2006.05
Post: #3
It's a bit confusing calling your GameView instance viewController, because there's a class called UIViewController. But that shouldn't affect how it works.

Are you sure your drawRect: method is correct? If you get it wrong it'll just call UIView's default implementation and your code will never get called, and there will be no compiler warning. It should be in the form of:

Code:
- (void)drawRect:(CGRect)rect

Or did you just shorten it to post here?

Also remember that setNeedsDisplay doesn't call drawRect: immediately, it waits until it returns to the run loop. If you're doing anything odd like running in your own loop it'll never get called.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2009.07
Post: #4
yes, sorry for the confusion. The Method is correct ->

Code:
- (void)drawRect:(CGRect)rect

What I do is, I check for a touch, i call a method from outside the class drawRect is in, to change some data. Within this method, I need the view to be redrawn, thats why I call setNeedsDisplay.

It seems like it's outside the main thread..... what to do?
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2009.07
Post: #5
Fot anyone still interested in this topic:

Send your message, that the view needs updating, then do

Code:
CFRunLoopRunInMode (CFRunLoopCopyCurrentMode(CFRunLoopGetCurrent()), 0, FALSE);

This forces the update on your view.
Hope it works for you...
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Taking advantage of 3DNow, SSE, MMX or whatever the PPC equivalent is called... Jones 3 3,565 Oct 24, 2006 07:06 AM
Last Post: Jones
  drawRect in NSOpenGLView help fakeOne 3 4,468 Sep 3, 2006 09:04 AM
Last Post: Xenos
  drawRect: is not executed after setNeedsDisplay:YES Moridin 7 8,367 Oct 19, 2005 12:42 AM
Last Post: Moridin
  drawRect before awakeFromNib unknown 4 4,832 Aug 25, 2005 04:17 PM
Last Post: unknown
  drawRect without resetting the NSView jspoon 4 4,099 Apr 1, 2005 09:38 AM
Last Post: jspoon