PDA

View Full Version : Drawing problems




Hans Kamp
Mar 26, 2013, 03:43 PM
I have the following problems with the program in the zip file:

When clicking on Teken and then on Rechthoek, "Rechthoek" is printed in the NSLOG part of the IDE but the yellow rectangle is not drawn immediately. It is drawn when:
- I go with the mouse cursor on the three "lights" at the left top of the window;
- I resize the window;
- it loses the focus (due to clicking on a different window for example the Xcode IDE or Google Chrome).

The rectangle changes in size when I resize the window, but that has been resolved after making the ZIP file.

Why isn't the rectangle displayed immediately? Why only after "touching" the window, resizing it or stealing the focus from it?



gnasher729
Mar 26, 2013, 08:32 PM
Without looking at your code, you probably need to call setNeedsDisplay:YES in the right place.

Hans Kamp
Mar 27, 2013, 12:57 AM
- (IBAction)rechthoekAction:(id)sender {
NSLog(@"Rechthoek"); // displays "Rechthoek" (Dutch for "Rectangle" in the log part of XCode (works)
rectDrawn = true; // flags redrawing for true (works) This will be checked in drawRect (works)
[self drawRectangle]; // draws the rectangle (doesn't draw it)
[self setNeedsDisplay:YES]; // ask for repaint (doesn't work)
}


In drawRect the flag is checked. When it is true (after Teken | Rechthoek) the rectangle is drawn each time I resize the window (not when I ask for it, setting rectDrawn to true and ask for repaint). But it should also be drawn on request.

gnasher729
Mar 27, 2013, 03:21 AM
- (IBAction)rechthoekAction:(id)sender {
NSLog(@"Rechthoek"); // displays "Rechthoek" (Dutch for "Rectangle" in the log part of XCode (works)
rectDrawn = true; // flags redrawing for true (works) This will be checked in drawRect (works)
[self drawRectangle]; // draws the rectangle (doesn't draw it)
[self setNeedsDisplay:YES]; // ask for repaint (doesn't work)
}


In drawRect the flag is checked. When it is true (after Teken | Rechthoek) the rectangle is drawn each time I resize the window (not when I ask for it, setting rectDrawn to true and ask for repaint). But it should also be drawn on request.

1. I <strongly> suggest naming object members like "rectDrawn" in a way that you can distinguish them from other variables. For example a member variable BOOL _rectDrawn with a property rectDrawn. For all I know there is a static variable rectDrawn somewhere. It's very helpful if you see code and _know_ what it does.

2. You shouldn't do any drawing in your action method, just the setNeedsDisplay. The rectangle should be drawn where you override drawRect:

Hans Kamp
Mar 27, 2013, 03:54 AM
1. I will change my own identifiers accordingly to your suggestion. The safest way is to use Dutch names, I guess, but it may make the code harder to read for others who don't know Dutch.

2. I have tested removing

[self drawRectangle];

But then the yellow rectangle isn't drawn at all until I resize the window. I wish it to be drawn after clicking on the menu item (and of course when resizing the window). After

[self setNeedsDisplay:YES];

I expect a redraw, but that doesn't happen. If it would, [self drawRectangle]; is abundant (drawing the rectangle twice, while once is enough).

robvas
Mar 27, 2013, 11:50 AM
You are not sending the setNeedsDisplay message to the object that you think you are.

What does the following code give you?

NSLog(@"%@", self);
Put it in both the rechthoekAction method and the drawRect method. See how they are different?

Create a mouseDown method that contains the same code as the rechthoekAction method.
-(void)mouseDown:(NSEvent *)event {
NSLog(@"mouseDown");
rectDrawn = true;
[self setNeedsDisplay:TRUE];
NSLog(@"md %@", self);
}

Then when you click the mouse inside your view, the rectangle will be draw right away, like you are looking for. Make sure you print the value of 'self' in that method as well.

Hans Kamp
Mar 27, 2013, 12:11 PM
Robvas,

What you suspected (the values of the two "selfs" being different) appears to be true.

But why is that the case? Why are there two objects although I think there is just one?

gnasher729
Mar 27, 2013, 12:22 PM
Robvas,

What you suspected (the values of the two "selfs" being different) appears to be true.

But why is that the case? Why are there two objects although I think there is just one?

Often happens when one object is created by your .xib file, and another is created by hand.

Hans Kamp
Mar 27, 2013, 12:30 PM
I have changed rechthoekAction into:

- (IBAction)rechthoekAction:(id)sender {
NSLog(@"Rechthoek: %@", self);
rectDrawn = true;
[self.drawingSpace setNeedsDisplay:YES];
[self setNeedsDisplay:YES];
}


2013-03-27 18:27:00.610 Drawing[384:303] SetFrameSize
2013-03-27 18:27:00.612 Drawing[384:303] Init
2013-03-27 18:27:00.612 Drawing[384:303] SetFrameSize
2013-03-27 18:27:00.632 Drawing[384:303] DrawRect: <Drawing: 0x1001247d0>
2013-03-27 18:27:13.121 Drawing[384:303] Rechthoek: <Drawing: 0x10012d9c0>
2013-03-27 18:27:13.123 Drawing[384:303] DrawRect: <Drawing: 0x1001247d0>
2013-03-27 18:27:34.115 Drawing[384:303] mouseDown
2013-03-27 18:27:34.115 Drawing[384:303] md <Drawing: 0x1001247d0>
2013-03-27 18:27:34.116 Drawing[384:303] DrawRect: <Drawing: 0x1001247d0>

This means that drawRect and mouseDown are referring to the same object, but differs from rechthoekAction.

Actually, the problem is solved. :)

gnasher729
Mar 28, 2013, 03:49 AM
This means that drawRect and mouseDown are referring to the same object, but differs from rechthoekAction.

Actually, the problem is solved. :)

If you have two views, the problem isn't solved. It's going to come back and bite you in the ****. Since you made a subclass anyway, add an initWithRect: method that just calls the superclass, set a breakpoint on it, and find out who is creating these two views. One of the callers is wrong.

Hans Kamp
Mar 28, 2013, 06:50 AM
Maybe I will delete the current project or let it be, and start a new one, and do it right from scratch. The new project must do the same as the current project, I am referring to.

The two views could be caused by moving the wrong object to the XIB designer instead of Objective-C class object (see the screenshot of Robvas in http://forums.macrumors.com/showthread.php?t=1561774).

And then I shouldn't make an Outlet of it.

It may be true that - because I am a newbie - I want to do things too fast.

Hans Kamp
Mar 28, 2013, 02:24 PM
I think Drawing2.zip shows how to do it. I have one object, as far as I can see.

self.myView.rectDrawn = YES;
[self.myView display];

to communicatie with myView, which is Outletted as follows:

@property (weak) IBOutlet Drawing2View *myView;

I think I am on the right track now.

Generally I think, I should order a book about Mac OS programming. What do you think it is a good book (which can be ordered by Amazon.com or (Dutch) Bol.com)? I want to order one.

ChOas
Mar 29, 2013, 04:01 AM
[..]

Generally I think, I should order a book about Mac OS programming. What do you think it is a good book (which can be ordered by Amazon.com or (Dutch) Bol.com)? I want to order one.

I started with 'Programming in Objective-C 2.0' (Kochan) and 'Cocoa Programming for Mac OS X' (Hillegas) (I *think* I got them from bol.com)

Lots has changed since then, but I'm sure they both have had revisions.

www.raywenderlich.com is a nice site with some tutorials.

Feel free to send me a private message if you have some questions in Dutch.

Hans Kamp
Mar 29, 2013, 10:06 AM
I started with 'Programming in Objective-C 2.0' (Kochan) and 'Cocoa Programming for Mac OS X' (Hillegas) (I *think* I got them from bol.com)

Lots has changed since then, but I'm sure they both have had revisions.

www.raywenderlich.com is a nice site with some tutorials.
Thanks. I will try to find out, whether the books and this URL is yet useful for me.

Feel free to send me a private message if you have some questions in Dutch.
Thanks.

I have ordered both of those books at Amazon.com. I want to have the books' most recent possible edition. :)