Drawing problems

Discussion in 'Mac Programming' started by Hans Kamp, Mar 26, 2013.

  1. Hans Kamp, Mar 26, 2013
    Last edited: Mar 28, 2013

    macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #1
    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:

  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Without looking at your code, you probably need to call setNeedsDisplay:YES in the right place.
     
  3. Hans Kamp, Mar 26, 2013
    Last edited: Mar 26, 2013

    thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #3
    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.
     
  4. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    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:
     
  5. thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #5
    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).
     
  6. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #6
    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.
     
  7. thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #7
    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?
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    Often happens when one object is created by your .xib file, and another is created by hand.
     
  9. Hans Kamp, Mar 27, 2013
    Last edited: Mar 27, 2013

    thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #9
    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. :)
     
  10. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    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.
     
  11. Hans Kamp, Mar 28, 2013
    Last edited: Mar 28, 2013

    thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #11
    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.
     
  12. thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #12
    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:

  13. macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #13
    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.
     
  14. Hans Kamp, Mar 29, 2013
    Last edited: Mar 29, 2013

    thread starter macrumors member

    Joined:
    Mar 24, 2013
    Location:
    Enschede, Netherlands
    #14
    Thanks. I will try to find out, whether the books and this URL is yet useful for me.

    Thanks.

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

Share This Page