Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Hans Kamp

macrumors member
Original poster
Mar 24, 2013
38
0
Enschede, Netherlands
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?
 

Attachments

  • Drawing.zip
    30.5 KB · Views: 70
Last edited:

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Without looking at your code, you probably need to call setNeedsDisplay:YES in the right place.
 

Hans Kamp

macrumors member
Original poster
Mar 24, 2013
38
0
Enschede, Netherlands
Code:
- (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.
 
Last edited:

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Code:
- (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

macrumors member
Original poster
Mar 24, 2013
38
0
Enschede, Netherlands
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

Code:
[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

Code:
[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

macrumors 68040
Mar 29, 2009
3,240
629
USA
You are not sending the setNeedsDisplay message to the object that you think you are.

What does the following code give you?
Code:
    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.
Code:
-(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

macrumors member
Original poster
Mar 24, 2013
38
0
Enschede, Netherlands
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

Suspended
Nov 25, 2005
17,980
5,565
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

macrumors member
Original poster
Mar 24, 2013
38
0
Enschede, Netherlands
I have changed rechthoekAction into:

Code:
- (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. :)
 
Last edited:

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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

macrumors member
Original poster
Mar 24, 2013
38
0
Enschede, Netherlands
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 https://forums.macrumors.com/threads/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.
 
Last edited:

Hans Kamp

macrumors member
Original poster
Mar 24, 2013
38
0
Enschede, Netherlands
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.
 

Attachments

  • Drawing2.zip
    27.7 KB · Views: 64

ChOas

macrumors regular
Nov 24, 2006
139
0
The Netherlands
[..]

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.

http://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

macrumors member
Original poster
Mar 24, 2013
38
0
Enschede, Netherlands
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.

http://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. :)
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.