1

Edit:

I'm drawing a line using drawRect: function which resides in a custom class in a view which I created in the MainStoryboard.storyboard file. The view does point to the custom class (draw2D) in the interface builder.

All code i have regarding draw in my main file(viewController) is now: (header file)

@class draw2D;  
@property (weak, nonatomic) IBOutlet draw2D *draw;  

(m file)

#import "draw2D.h"  
[draw.listOfFreqToDraw addObject: closestChar];   
[draw setNeedsDisplay];

The problem I seem to have now is that it only runs the drawRect method in draw2D class once and never calls it again (therefor listOfFreqToDraw isn't even called after the first run)

Daniel
  • 23,129
  • 12
  • 109
  • 154
Himmi
  • 47
  • 6
  • Did you update the "Class" name to your custom class in Interface Builder? – Peter Pajchl Aug 10 '12 at 12:10
  • Yep, I've done that. The custom class called draw2D is linked to it. – Himmi Aug 10 '12 at 12:18
  • Like I mentioned the class is drawing just fine, the problem I'm having is sharing data information with the drawing class. – Himmi Aug 10 '12 at 12:19
  • can you post the code where are you passing the data into the view? – Peter Pajchl Aug 10 '12 at 12:20
  • I think that's something that I'm doing really really wrong and I'm not sure how to do it better. I call the subclass which I have initiated as draw. And then I just call the variable array and fill it with items. But then I call refresh on the view [draw.listOfFreqToDraw addObject: closestChar]; [self.noteLineView setNeedsDisplay]; – Himmi Aug 10 '12 at 12:28
  • draw is a instance of the custom class and noteLineView is a instance of UIiew connected via interface builder. I've seen by using the debugger draw send information over to the custom class but then noteLineView being refreshed with an empty array (which just had item(s) placed inside it. – Himmi Aug 10 '12 at 12:31
  • make sure listOfFreqToDraw is "strong" – Amitay Aug 10 '12 at 12:33
  • Aye, the array keeps filling up with each object added but always seems empty in between when I call setNeedsDisplay. – Himmi Aug 10 '12 at 12:39
  • Then please post your code... Its hard for us to help you this way... – Amitay Aug 10 '12 at 12:41
  • Changed it from retain to strong, still have the same problem, sorry about not posting much code, it was either that or take bits here and there and I thought it would have looked stupid (it's my first question here, thanks for the replies) – Himmi Aug 10 '12 at 12:43
  • Its fine everyone has a first time :) We are all waiting for your code... just edit your original Question – Amitay Aug 10 '12 at 12:48

1 Answers1

0

You can always change a variable in draw2D

Set this up in the draw2D.h

@property (nonatomic, strong) NSMutableArray * listOfFreqToDraw;

in draw2D.m

- (void)drawRect:(CGRect)rect {
 for (UIBezierPath *path in self. listOfFreqToDraw) {
    [path stroke]; 
   }
}

and in your main class

[draw.listOfFreqToDraw addObject: closestChar];
[draw setNeedsDisplay];
Amitay
  • 468
  • 5
  • 20
  • In noteLineView.paths = self.paths; Property 'paths' not found on object UIView, did you mean the draw2D class? It can't seem to find properties that the UIView Custom Class holds, and if I use the direct class it seems to use another instance of it. – Himmi Aug 10 '12 at 12:48
  • When you wrote [draw.listOfFreqToDraw addObject: closestChar]; [self.noteLineView setNeedsDisplay] What object is of type draw2D? – Amitay Aug 10 '12 at 12:52
  • sorry about confusing draw2D and draw. draw is: draw = [[draw2D alloc] init]; and draw2D is just the custom class which is imported – Himmi Aug 10 '12 at 12:54
  • I could see the code you wrote working if my noteLineView which is just a UIView could actually point to a instance of the custom class and its variables which It does not seem to do – Himmi Aug 10 '12 at 12:56
  • I don't understand the point of noteLineView. just do [draw setNeedsDisplay]; – Amitay Aug 10 '12 at 13:06
  • Ok my bad, give me a sek, I'm tweeking it a little – Himmi Aug 10 '12 at 13:09
  • Where is drawRect implemented? and what is the class of the view you set in Interface Builder? – Amitay Aug 10 '12 at 13:10
  • If i do [draw setNeedsDisplay]; it calls the drawing once and draws out something if it exist in the first run. Then if I fill in the array it never seems to go back to the drawRect function which is inside the draw2D subclass (which it did with the noteLineView). In the interface builder draw2D is the custom class connected to this view edit: it seems to just draw when I init the draw class and never does anything when [draw setNeedsDisplay]; is called – Himmi Aug 10 '12 at 13:14
  • draw should be setup as @property (weak, nonatomic) IBOutlet draw2D *draw; – Amitay Aug 10 '12 at 13:19
  • and make sure you initialize the listOfFreqToDraw in draw2D -(NSMutableArray *)listOfFreqToDraw { if(!listOfFreqToDraw) listOfFreqToDraw = [[NSMutableArray alloc]init]; return listOfFreqToDraw; } – Amitay Aug 10 '12 at 13:21
  • I had to delete a comment to post a new one, I'm at the limit and I cant "move the discussion to chat" since I'm a new user. I updated the whole question to a much shorter one, and I hope it's in a simpler form now as I am very confused myself. Thanks for the help so far :) – Himmi Aug 10 '12 at 14:30
  • I had the same problem and it was fixed by setting listOfFreqToDraw like this: `@property (nonatomic, strong) NSMutableArray * listOfFreqToDraw;` Many people have done this without problems... You should upload your **full** project to a website and link it here.... – Amitay Aug 11 '12 at 20:10