0

sorry for bothering you again about the - (void)setNeedsDisplay which does not call - (void)drawRect: method...but I spend so much time on this problem

I am a beginner in Objective-C and I am trying to do a simple shoot-them up. (I know I need to work)

But now, I just want to raise a picture up in the view. For example, the picture appears at (0,0) of the view and I would like this to make this picture go up (of 10 pixels) each time I press a NSButton.

The problem is the picture does not move ;( Some of you can check this out ? Here is the code:

#import <Cocoa/Cocoa.h>


@interface maVue : NSView {

    NSImageView * monMonstre;
    int nombre;
}
@property (readwrite) int nombre;

- (IBAction)boutonClic:(id)sender;

@end








#import "maVue.h"


@implementation maVue


- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
        nombre = 2;
        monMonstre = [[NSImageView alloc]init];
    }
    return self;
}


- (void)drawRect:(NSRect)dirtyRect 
{
    // Drawing code here.
    [monMonstre setFrame:CGRectMake(0,[self nombre],100,100)];
    [monMonstre setImage:[NSImage imageNamed:@"monstre.jpg"]];
    [self addSubview:monMonstre];
}


- (IBAction)boutonClic:(id)sender
{
    [self setNombre:[self nombre]+10];
    [self setNeedsDisplay:YES];

}


- (void)setNombre:(int)nouveauNombre
{
    nombre=nouveauNombre;
}

- (int)nombre
{
    return nombre;
}
@end
Dennis Pashkov
  • 934
  • 10
  • 24

1 Answers1

0

You DO NOT NEED - (void)setNeedsDisplay!

Just use standart for NSView property frame.

You should rewrite your code so:

#import <Cocoa/Cocoa.h>

@interface maVue : NSView
{
  NSImageView * monMonstre;
  int nombre;
}
@property (readwrite) int nombre;

- (IBAction)boutonClic:(id)sender;

@end


#import "maVue.h"

@implementation maVue

- (void)initWithFrame:(CGRect)frame
{
  if(self = [super initWithFrame:frame])
  {
    nombre = 2;
    monMonstre = [[NSImageView alloc] init];
    [monMonstre setImage:[NSImage imageNamed:@"monstre.jpg"]];
    NSSize mSize = [monMonstre image].size;
    NSRect monstreFrame;
    monstreFrame = NSMakeRect(0.0f, [self nombre], mSize.width, mSize.height);
    [monMonstre setFrame:monstreFrame];
    [self addSubview:monMonstre];
    [monMonstre release]; // <-- only if you don't use ARC (Automatic Reference Counting)
  }
  return self;
}

- (IBAction)boutonClic:(id)sender
{
  [self setNombre:[self nombre]+10];

  NSRect frame = [monMonstre frame];
  frame.origin.y = [self nombre];

  [monMonstre setFrame:frame]
}

- (void)setNombre:(int)nouveauNombre
{
  nombre=nouveauNombre;
}

- (int)nombre
{
  return nombre;
}

@end
Dennis Pashkov
  • 934
  • 10
  • 24
  • thanks for your quick reply Денис Пашков ! and it's a good idea to use -frame . but the picture does not appear anymore because you delete the -drawRect method... If I add the -drawRect method, the picture appear but still does not move when I click on the NSButton... – Maatthieu Maat Apr 24 '13 at 22:40
  • and if I try to put an [self addSubview:monMonstre] inside the -boutonClic method, the picture won't show.. – Maatthieu Maat Apr 24 '13 at 22:46
  • You need to add your 'monMonstre' only one time when you init it. If you want it to be shown, you should set image to it) – Dennis Pashkov Apr 25 '13 at 00:01
  • Maatthiew Maat, I've changed my answer. Look at initWithFrame: method. – Dennis Pashkov Apr 25 '13 at 00:17
  • Hello Денис Пашков ! I just copy paste your code..but the picture still does not move. It's crazy. Thanks anyway for your help – Maatthieu Maat Apr 25 '13 at 08:39
  • Check if your -(void)boutonClic: gets called – Dennis Pashkov Apr 25 '13 at 09:00
  • all the code are in a subclass of NSView. I tried your code in a subclass of NSObject and it worked ! the problem is in the future i will need NSView methods like -(void)mouseMoved... So I need to write the code in a NSView class, but why it does not work only in it ? I suspect there is something to do with the -drawRect method. What do you think ? – Maatthieu Maat Apr 25 '13 at 09:16
  • oh I found why.. the -(void)boutonClic: get called but inside another instance of myView but with the same name.. – Maatthieu Maat Apr 25 '13 at 11:59