|
|
#1 |
|
CG on iPhone question
I'm trying to follow along with the guides and I thought I would be creating a rectangle with the following code in my DrawBox.m:
Code:
- (void)drawRect:(CGRect)rect {
// Drawing code here.
CGContextRef myContext = UICurrentContext();
CGRect ourRect = CGRectMake(40, 40, 240, 120);
CGContextSetRGBFillColor(context, 0.0,0.0,1.0,1.0);
CGContextFillRect(context, ourRect);
CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextStrokeRectWithWidth(context, ourRect, 10);
}
Thanks! P.S. I am fairly new to Objective-C and I know a lot of Java and Python, so if anyone could show me a little bit of what I'm doing in Java with declaring context that would be great, too.
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com |
|
|
|
0
|
|
|
#2 |
|
Are you including the files that Context is set in?
# include <asdf.h> (replace asdf.h with the appropriate header file)
__________________
Apple and Dell are the only ones in this industry making money. They make it by being Wal-Mart. We make it by innovation, - Steve Jobs
The Tegian Zone-Glass Onion Radio |
|
|
|
0
|
|
|
#3 |
|
Context isn't set. I'm including the header file, but I'm not sure what to do with 'context'.
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com |
|
|
|
0
|
|
|
#4 |
|
Context must contain a value from somewhere. Either passed in from a call of this method or set in the method.
TEG
__________________
Apple and Dell are the only ones in this industry making money. They make it by being Wal-Mart. We make it by innovation, - Steve Jobs
The Tegian Zone-Glass Onion Radio |
|
|
|
0
|
|
|
#5 |
|
You need to use the myContext variable, which you set using UICurrentContext(), instead of "context".
__________________
Obama is a true statesman whose experience as a state senator, half-term US Senator & guest lecturer in a Constitutional Law class has fully prepared him to take control of our nuclear arsenal.-Me |
|
|
|
0
|
|
|
#6 | ||
|
Changing myContext up in:
Code:
CGContextRef context = UICurrentContext(); Code:
- (void)drawRect:(CGRect)rect {
// Drawing code here.
CGContextRef context = UICurrentContext();
CGRect ourRect = CGRectMake(40, 40, 240, 120);
CGContextSetRGBFillColor(context, 0.0,0.0,1.0,1.0);
CGContextFillRect(context, ourRect);
CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextStrokeRectWithWidth(context, ourRect, 10);
Quote:
Quote:
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com |
|||
|
|
0
|
|
|
#7 |
|
Is the class a subclass of UIView? Everything else looks right to me. I don't think you need to import anything for core graphics like you would for Mac OS X development - everything should come with UIView. Details about the header file might be helpful here.
Everything else looks like it should based on the UIView documentation. Best of luck |
|
|
|
0
|
|
|
#8 |
|
It sounds like UIView.h is not being imported anywhere.
|
|
|
|
0
|
|
|
#9 |
|
Try this it works for me
CGContextRef context = UICurrentContext(); change to CGContextRef context = UIGraphicsGetCurrentContext(); |
|
|
|
0
|
|
|
#10 |
|
I got it working, but i get this warning and I can't fix it. "warning:local declaration of 'myContext' hides instance variable."
and here is my code Code:
CGContextRef myContext = UIGraphicsGetCurrentContext();
// Draw the intermediate lines
CGContextSetGrayStrokeColor(myContext, 1, 1.0);
CGContextBeginPath(myContext);
for (UIView *viewAtIndex in self.subviews){
UIView *tempView = [self.subviews objectAtIndex:0];
CGContextMoveToPoint(myContext,tempView.center.x ,tempView.center.y);
tempView = [self.subviews objectAtIndex:1];
CGContextAddLineToPoint(myContext, tempView.center.x,tempView.center.y);
CGContextStrokePath(myContext);
|
|
|
|
0
|
|
|
#11 |
|
Have you defined myContext as an instance variable in your class interface by any chance?
|
|
|
|
0
|
|
|
#12 | |
|
Do you have myContext declared in your header file interface? If so, it should be removed.
Best of luck Quote:
|
||
|
|
0
|
|
|
#13 |
|
Not to be a jerk, but if people are having trouble understanding what an undeclared variable is, or how and where variables should be declared, it might be a good idea to step away from the SDK for a bit and just do some simple Objective-C programming that removes the complexities of the SDK and allows you to get the hang of general issues first.
|
|
|
|
0
|
|
|
#14 |
|
I think the SDK is a great way to have some fun and experiment for us that never programmed in Xcode before. But I do agree with you that it's a good way to learn from the ground up, and that's why I picked up Cocoa programming for OS X today.
|
|
|
|
0
|
|
|
#15 |
|
I've done my share of desktop programming. I'm just kinda new to Objective-C. Not really "new" just don't use it a lot so I fall into Java habits and get confused.
However, with the iPhone SDK I will be using it more now. Here's what I have now and it builds successfully but doesn't draw the actual box like I'd like it to. I'm hoping after I figure out how to draw the box I'll make it watch for a tap and when the box is tapped it'll change colors or something. Just learning the ropes! Anyway, here it is... This is in MyView.m: Code:
- (void)drawBox:(CGRect)rect {
// Drawing code here.
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect box = CGRectMake(40, 40, 240, 120);
CGContextSetRGBFillColor(context, 0.0,0.0,1.0,1.0);
CGContextFillRect(context, box);
CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextStrokeRectWithWidth(context, box, 10);
}
Code:
@interface MyView : UIView {
}
-(void)DisplayLabel;
-(void)drawBox:(CGRect)rect;
Code:
// Show the label [contentView DisplayLabel]; // Show the box CGRect rect = CGRectMake(00, 00, 320, 480); [contentView drawBox:rect]; Any ideas as to why the box isn't showing up?
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com |
|
|
|
0
|
|
|
#16 |
|
If I'm understanding the question correctly the snippet above in drawRect is from MyView.m
Here is the whole file: Code:
//
// MyView.m
// ClickBox
//
// Created by ########## on 3/25/08.
// Copyright __MyCompanyName__ 2008. All rights reserved.
//
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code here.
}
return self;
}
-(void) DisplayLabel {
CGRect frame = CGRectMake(00,20,320,40);
UILabel *label = [[ UILabel alloc ] initWithFrame: frame ];
label.textAlignment = UITextAlignmentCenter;
label.text = @"Click it!";
label.font = [ UIFont boldSystemFontOfSize: 25.00 ];
label.textColor = [ UIColor colorWithRed: 100.0/255.0 green: 255.0/255.0 blue: 100.0/255.0 alpha: 1.0 ];
label.backgroundColor = [ UIColor clearColor ];
[self addSubview: label];
[ label release ];
}
- (void)drawBox:(CGRect)rect {
// Drawing code here.
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect box = [self bounds];
CGContextSetRGBFillColor(context, 0.0,0.0,1.0,1.0);
CGContextFillRect(context, box);
CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextStrokeRectWithWidth(context, box, 10);
}
@end
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com |
|
|
|
0
|
|
|
#17 |
|
Ok, so I changed my myView.m part to this:
Code:
- (void)drawBox {
// Drawing code here.
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect box = CGRectMake(40, 40, 240, 120);
CGContextSetRGBFillColor(context, 0.0,0.0,1.0,1.0);
CGContextFillRect(context, box);
CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextStrokeRectWithWidth(context, box, 10);
}
I'm a bit confused as to why it isn't working. I'll keep trying stuff and see what happens.
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com |
|
|
|
0
|
|
|
#18 |
|
Thanks for the helpful tip!
With this now placed in my app deletgate.m it works, but is there a more efficient way? Code:
CGRect rect = CGRectMake(40, 40, 240, 120); // Show the box [contentView drawRect:rect];
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com |
|
|
|
0
|
|
|
#19 |
|
Oh. Well... no code is more efficient than wasteful useless code for sure!
Well, thanks a ton for the help!
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com |
|
|
|
0
|
|
|
#20 |
|
Great, glad you got it working
|
|
|
|
0
|
|
|
#21 |
|
Yeah! That part is working great, now it's onto more!
I'm going to make it so when you touch it displays a blip right where you touched. Just so I understand cocoa touch a bit better. I'm sure I'll have more questions. For one, my controller doesn't seem to be included. I went to file and added a new UIViewController subclass which I called ClickControl. And so I added this to the ClickBoxAppDelegate.m: Code:
#import "ClickBoxAppDelegate.h" #import "MyView.h" #import "ClickControl.h" The only reason it would seem it isn't including it is because I have this:Code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com |
|
|
|
0
|
|
|
#22 |
|
See this thread for getting the rotation to work.
You say your controller isn't included. How are you creating it? You need to initialized an instance of the ClickControl class somewhere in your code. |
|
|
|
0
|
|
|
#23 | |
|
Quote:
Code:
//
// ClickControl.m
// ClickBox
//
// Created by ######## on 3/25/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "ClickControl.h"
@implementation ClickControl
/*
- (id)init
{
if (self = [super init]) {
// Initialize your view controller.
self.title = @"ClickControl";
}
return self;
}
*/
- (void)loadView
{
// Create a custom view hierarchy.
UIView *controllerView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
controllerView.autoresizesSubviews=YES;
controllerView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
self.view = controllerView;
// the view hierarchy is now retaining the view, so we can release it here
[controllerView release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
/*
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview.
// Release anything that's not essential, such as cached data.
}
- (void)dealloc
{
[super dealloc];
}
}
*/
@end
If there is other code that will help, let me know.
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com |
||
|
|
0
|
|
|
#24 | |
|
Same problem ! please help
Quote:
I have exactly the same problem. How did u solve the error? Line Location Tool:0: collect2: ld returned 1 exit status Line Location Tool:0: symbol(s) not found Line Location Tool:0: -[TestView drawRect:] in TestView.o Line Location Tool:0: "_CGContextFillRect", referenced from: Line Location Tool:0: -[TestView drawRect:] in TestView.o Line Location Tool:0: "_CGContextSetRGBFillColor", referenced from: here is my Code TestView.h Code:
#import <UIKit/UIKit.h>
@interface TestView : UIView {
}
@end
Code:
#import <UIKit/UIKit.h>
#import <CoreFoundation/CoreFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import "TestView.h"
@implementation TestView
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGRect aRect;
aRect.origin.x=0;
aRect.origin.y=0;
aRect.size.width=20;
aRect.size.height=20;
UIRectFill(aRect);
//Get Window Graphics Context
CGContextRef aContext= UIGraphicsGetCurrentContext();
//Draw on the Layer
aRect.origin.x=20;
aRect.origin.y=20;
aRect.size.width=100;
aRect.size.height=100;
CGContextSetRGBFillColor(aContext, 255.0, 0.0,0.0,1.0);
CGContextFillRect(aContext,aRect);
}
- (void)dealloc {
[super dealloc];
}
@end
I have tried adding all the Header Files ..nothing seems to work. Please help me understand. |
||
|
|
0
|
|
|
#25 | |
|
Solved!!! Finally after 14days!!
Quote:
The problem :/ The damn framework. I had to add the framework CoreGraphics which is kept in the worst possible location 15 directories below the root directory. I believe this is the worst directory hierarchy ever in programming history.. ONe needs to select the Add the FrameWork "CoreGraphics" to the Project To get all CG functions working which i my opinion is very lame. It should just be an import of CoreGraphics that should really do the trick. And this has not been mentioned anywhere in the goddamn API documentations. Ohh Btw u wont find the coregraphics framework if u try searching the directories..best is to open the QuartzDemo project and look that FrameWork "Coregraphics" and open in Finder. Then follow the Directory and u will eventually find it..do the same for ur project. Not that i have let my anger out.... I think as i am new to MAC OS X its very valid that i found it so hard. Sorry for any anger venting ...:P just needed to get it out of my system. |
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| YouTube veiwing on iPhone question | ILBandit | iPhone Tips, Help and Troubleshooting | 7 | Jan 27, 2010 02:37 PM |
| Installed Contra on my NES on iPhone question | SandersHokie | Jailbreaks and iOS Hacks | 1 | Aug 28, 2008 03:32 PM |
| Using Gmail on iPhone question. | adauge | iPhone | 4 | May 14, 2008 05:19 PM |
| Small IMAP on iPhone question | GuillaumeB | iPhone | 3 | Nov 4, 2007 01:55 AM |
| Safari on iPhone Question | AkiNikoTanTan | iPhone | 7 | Sep 7, 2007 01:56 AM |
All times are GMT -5. The time now is 06:20 AM.












The only reason it would seem it isn't including it is because I have this:
Linear Mode

