Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 26, 2013, 03:43 PM   #1
Hans Kamp
macrumors member
 
Join Date: Mar 2013
Location: Enschede, Netherlands
Drawing problems

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?
Attached Files
File Type: zip Drawing.zip (30.5 KB, 14 views)

Last edited by Hans Kamp; Mar 28, 2013 at 08:14 AM. Reason: Not resolved yet; see last few messages.
Hans Kamp is offline   0 Reply With Quote
Old Mar 26, 2013, 08:32 PM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Without looking at your code, you probably need to call setNeedsDisplay:YES in the right place.
gnasher729 is offline   0 Reply With Quote
Old Mar 27, 2013, 12:57 AM   #3
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: 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 by Hans Kamp; Mar 27, 2013 at 01:03 AM.
Hans Kamp is offline   0 Reply With Quote
Old Mar 27, 2013, 03:21 AM   #4
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Hans Kamp View Post
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:
gnasher729 is offline   1 Reply With Quote
Old Mar 27, 2013, 03:54 AM   #5
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: 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).
Hans Kamp is offline   0 Reply With Quote
Old Mar 27, 2013, 11:50 AM   #6
robvas
macrumors 68000
 
Join Date: Mar 2009
Location: 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.
robvas is offline   1 Reply With Quote
Old Mar 27, 2013, 12:11 PM   #7
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: 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?
Hans Kamp is offline   0 Reply With Quote
Old Mar 27, 2013, 12:22 PM   #8
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Hans Kamp View Post
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.
gnasher729 is offline   1 Reply With Quote
Old Mar 27, 2013, 12:30 PM   #9
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: 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 by Hans Kamp; Mar 27, 2013 at 12:38 PM. Reason: Gnasher gave explanation to why it is different.
Hans Kamp is offline   0 Reply With Quote
Old Mar 28, 2013, 03:49 AM   #10
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Hans Kamp View Post
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.
gnasher729 is offline   1 Reply With Quote
Old Mar 28, 2013, 06:50 AM   #11
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: 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 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.

Last edited by Hans Kamp; Mar 28, 2013 at 09:07 AM.
Hans Kamp is offline   0 Reply With Quote
Old Mar 28, 2013, 02:24 PM   #12
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: 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.
Attached Files
File Type: zip Drawing2.zip (27.7 KB, 9 views)
Hans Kamp is offline   0 Reply With Quote
Old Mar 29, 2013, 04:01 AM   #13
ChOas
macrumors regular
 
Join Date: Nov 2006
Location: The Netherlands
Quote:
Originally Posted by Hans Kamp View Post

[..]

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.
ChOas is offline   0 Reply With Quote
Old Mar 29, 2013, 10:06 AM   #14
Hans Kamp
Thread Starter
macrumors member
 
Join Date: Mar 2013
Location: Enschede, Netherlands
Quote:
Originally Posted by ChOas View Post
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.

Quote:
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 by Hans Kamp; Mar 29, 2013 at 01:04 PM. Reason: Actually ordered them.
Hans Kamp is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPad 4 for Drawing ndstrenge iPad Tips, Help and Troubleshooting 2 Jun 24, 2013 10:52 PM
Drawing game iRock1 iPad Apps 0 Oct 31, 2012 10:33 AM
Is there a drawing app brilliant iPad Apps 1 Aug 20, 2012 06:36 AM
drawing software Alaska1man iMac 6 Jun 10, 2012 04:27 PM

Forum Jump

All times are GMT -5. The time now is 04:51 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC