开发者

Help with Xcode drawRect using textfield to input value

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();

    CGFl开发者_如何学编程oat 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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜