1
votes

i am currently trying to do a bar graph in XCode. I have tried CPGraph and all the stuff around but they are all out of date and i need help for XCode 4. I am completely newb with this and that's why i need your help.

Here is my code:

-(void)drawRect:(CGRect)rect

{

NSString *s = textField.text;
value = [s intValue];
NSLog(@"%i",value);


    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 60);

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

    CGFloat components[] = {0.0, 0.0, 1.0, 1.0};

    CGColorRef color = CGColorCreate(colorspace, components);

    CGContextSetStrokeColorWithColor(context, color);

    CGContextMoveToPoint(context, 400, value);
    CGContextAddLineToPoint(context, 400, 100);

    CGContextStrokePath(context);
    CGColorSpaceRelease(colorspace);
    CGColorRelease(color);

}

Notice that i have changed one of the point value for "value". What i have done is create a UITextField and i want to be able to write like 500 in the UITextField and that the bar adjust itself. Currently the NSLog tell me that value is equal to 0 even if, before building my app i manually enter a number in the TextField. I have been searching fo 3 days now and everything i found give me errors and are incomplete as i says i know almost nothing about objective-c in xcode. I also noticed during my search that this type of line doesn't refresh real time if you dont tell him. I would like help with that too if that's part of my problem. If you want more information on my code just tell me. I will be really grateful if somebody can help me.

here is my .h :

@interface draw2D : UIView

{

 UITextField *textField;

}

@property (nonatomic, retain) IBOutlet UITextField *textField;

@end

2
I'm assuming that you have connected the text field to an IBOutlet in Interface builder?Merky
here is what i did : @interface draw2D : UIView { UITextField *textField; } @property (nonatomic, retain) IBOutlet UITextField *textField; @endproticsam
Typically if you are using interface builder (IB) to bind your text field (or anything for that matter) via an IBOutlet, you do not need to add it as a property. So I'm assuming you used IB to connect the text field to your instance you have declared above right? Without doing that your text field will be nil.Merky
instance? the only thing that is linked to my textField is under referencing outlets and its file's owner. as i said i dont really understand what im doingproticsam
OK, then yea, you have that right it sounds like. Don't know, the code looks right (though I have not run it), I would suggest printing out the string in the text field: NSLog(@"%@", s); and see if that is what you think it is. Also, I do not see a declaration for "value". Make sure it is: int value = [s intValue]; I'm out for the day so I'll check in on this tomorrow.Merky

2 Answers

0
votes

OK, it looks like since you have your text field hooked up in IB to File's Owner, that is typically your app delegate which is not the class you have above as that is a UIView. So, not unless you changed File's Owner to this UIView then your text field is not hooked up. So, typically what I do is to add a UIView to the xib window in IB (if you are not using the one that comes free when you create a XIB). Then that is what I'll attach the text box to, not File's Owner. Kind of begs the question of how anything is attached to Files Owner not unless you have a UITextField declared in there too (again providing you didn't change the class from the app delegate to this view class).

I know that if someone is new to this is is almost like greek. I developed in Java, C/C++, C#, etc for years and this was like learning how to program all over again.

If you are new to this I highly suggest the iPhone dev book by Big Nerd Ranch. I found it very useful.

0
votes

OK, I found out how to redraw my line with a button refresh.

What I did was create a button and link it with my UIView where my line is being drawn. Inside I wrote [self setNeedsDisplay]; and each time I click the button it refreshes the view and gives me back my NSlog that I set in the same method as my drawRect.

Many thanks again Merky.