0

i have some instance methods implemented in a class to do some 2D painting; and the image is rendered into a view declared in my paintViewController.

these methods (like clearImage, changeBrushColor) are fully working if i call them from code generated buttons inside the same class.

but what i want is to use the UIkit to make all the graphic, buttons ecc and just an empty view to paint in.

i've tried some ways to call instance methods from other classes but none of them seems to be working..

can someone explain to me step by step how to do this?

These are the ways i've already tried:

  1. in PaintViewController.m I #import "Paint.h" then call the erase method like this

    - (IBAction)eraseButton:(id)sender {
    
         Paint *newInstance = [[Paint alloc]init];
         [newInstance clearImage];
    }
    

    it is like the method is called but not working.. (if i put an NSLog inside the method it prints stuff)

  2. i @class Paint inside my PaintViewController.h, set the property in interface as @property (strong, nonatomic) Paint *paint; and inside the IBAction i write [self.class clearImage];

this is not working at all.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dodgson86
  • 333
  • 1
  • 4
  • 15
  • 1
    Do you mean `[self.paint clearImage]`? – tc. Jun 11 '12 at 17:21
  • what is your Paint object? Is it a view of your viewController? And what is the code of your clearImage method? – Morion Jun 11 '12 at 17:21
  • Assuming that `Paint` is your painting view, have you connected the `paint` property in `PaintViewController` to the view in your XIB/Storyboard through an IBOutlet (and set that view in IB to the subclass Paint in the inspector)? –  Jun 12 '12 at 12:37

1 Answers1

0

If I read your question correctly, what you need is to implement the delegate method. It is commonly used pattern in Objective-C in this scenario. I have a similar answer with sample code in this SO post.

Community
  • 1
  • 1
user523234
  • 14,323
  • 10
  • 62
  • 102
  • I tried your way to delegate and did not worked... let me see if i have understand it well; in my situation classA is the Paint class in which are coded all the painting functions like clearImage and classB is the paintViewController where the UIbuttons are? – Dodgson86 Jun 12 '12 at 10:12
  • I am not familiar with Cocos2d which the CCLayer is from. But it should not prevent it from using the same technique. Is your Paint a subclass of CCLayer? You can also try using NSNotification. – user523234 Jun 15 '12 at 22:47
  • Just re-looked at your code: Paint *newInstance = [[Paint alloc]init]; [newInstance clearImage]; These will not work. Because newInstance is not the same as whatever the current Paint instance that you wanted to clearImage. – user523234 Jun 15 '12 at 22:56