Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 19, 2008, 02:01 PM   #1
Buschmaster
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
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);
}
However, I get an error pertaining to the use of 'context.' It says it is undeclared but I'm not sure where to declare it or how. Should I declare it in DrawBox.h? The guides are confusing me even more so, so hopefully one of you could help me and I can continue on my way of learning?

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
Buschmaster is offline   0 Reply With Quote
Old Mar 19, 2008, 02:06 PM   #2
TEG
macrumors 604
 
TEG's Avatar
 
Join Date: Jan 2002
Location: Langley, Washington
Send a message via ICQ to TEG Send a message via AIM to TEG Send a message via MSN to TEG Send a message via Yahoo to TEG Send a message via Skype™ to TEG
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
TEG is offline   0 Reply With Quote
Old Mar 19, 2008, 02:15 PM   #3
Buschmaster
Thread Starter
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
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
Buschmaster is offline   0 Reply With Quote
Old Mar 19, 2008, 02:21 PM   #4
TEG
macrumors 604
 
TEG's Avatar
 
Join Date: Jan 2002
Location: Langley, Washington
Send a message via ICQ to TEG Send a message via AIM to TEG Send a message via MSN to TEG Send a message via Yahoo to TEG Send a message via Skype™ to TEG
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
TEG is offline   0 Reply With Quote
Old Mar 19, 2008, 02:23 PM   #5
Sayer
macrumors 6502a
 
Sayer's Avatar
 
Join Date: Jan 2002
Location: Austin, TX
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
Sayer is offline   0 Reply With Quote
Old Mar 19, 2008, 02:39 PM   #6
Buschmaster
Thread Starter
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
Changing myContext up in:
Code:
CGContextRef context = UICurrentContext();
Making the whole thing:
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);
Gives me two warnings and a build error:

Quote:
Warning: Implicit declaration of function UICurrentContext
Warning: Initialization makes pointer from integer without a cast
and
Quote:
"_UICurrentContext", referenced from:
-[DrawBox drawRect:] in DrawBox.o
symbol(s) not found
collect2: id returned, 1 exit status
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com
Buschmaster is offline   0 Reply With Quote
Old Mar 19, 2008, 08:47 PM   #7
lucasgladding
macrumors 6502
 
Join Date: Feb 2007
Location: Waterloo, Ontario
 
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
lucasgladding is offline   0 Reply With Quote
Old Mar 19, 2008, 10:07 PM   #8
admanimal
macrumors 68040
 
Join Date: Apr 2005
It sounds like UIView.h is not being imported anywhere.
admanimal is offline   0 Reply With Quote
Old Mar 20, 2008, 12:56 AM   #9
diehard89
macrumors newbie
 
Join Date: Mar 2008
Location: Singapore
Try this it works for me

CGContextRef context = UICurrentContext();

change to

CGContextRef context = UIGraphicsGetCurrentContext();
diehard89 is offline   0 Reply With Quote
Old Mar 20, 2008, 07:33 AM   #10
Alfons
macrumors newbie
 
Join Date: Mar 2008
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);
Anyone?
Alfons is offline   0 Reply With Quote
Old Mar 20, 2008, 07:51 AM   #11
BigJimSlade
macrumors member
 
Join Date: Dec 2005
Have you defined myContext as an instance variable in your class interface by any chance?
BigJimSlade is offline   0 Reply With Quote
Old Mar 20, 2008, 07:52 AM   #12
lucasgladding
macrumors 6502
 
Join Date: Feb 2007
Location: Waterloo, Ontario
 
Do you have myContext declared in your header file interface? If so, it should be removed.

Best of luck

Quote:
Originally Posted by Alfons View Post
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);
Anyone?
lucasgladding is offline   0 Reply With Quote
Old Mar 20, 2008, 10:08 AM   #13
admanimal
macrumors 68040
 
Join Date: Apr 2005
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.
admanimal is offline   0 Reply With Quote
Old Mar 20, 2008, 12:44 PM   #14
Alfons
macrumors newbie
 
Join Date: Mar 2008
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.
Alfons is offline   0 Reply With Quote
Old Mar 25, 2008, 09:43 PM   #15
Buschmaster
Thread Starter
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
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);
	
	
}
And MyView.h:
Code:
@interface MyView : UIView {

}

-(void)DisplayLabel;
-(void)drawBox:(CGRect)rect;
And my delegate.m:
Code:
	// Show the label
	[contentView DisplayLabel];
	
	// Show the box
	CGRect rect = CGRectMake(00, 00, 320, 480);	
	[contentView drawBox:rect];
delegate.h remains the same.

Any ideas as to why the box isn't showing up?
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com
Buschmaster is offline   0 Reply With Quote
Old Mar 25, 2008, 10:24 PM   #16
Buschmaster
Thread Starter
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
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
Buschmaster is offline   0 Reply With Quote
Old Mar 25, 2008, 10:42 PM   #17
Buschmaster
Thread Starter
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
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);
	
}
and changed the other appropriate parts to now match it and still.... nothing.

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
Buschmaster is offline   0 Reply With Quote
Old Mar 25, 2008, 11:12 PM   #18
Buschmaster
Thread Starter
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
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];
Off to read the suggested homework!
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com
Buschmaster is offline   0 Reply With Quote
Old Mar 25, 2008, 11:17 PM   #19
Buschmaster
Thread Starter
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
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
Buschmaster is offline   0 Reply With Quote
Old Mar 25, 2008, 11:30 PM   #20
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Great, glad you got it working
kainjow is offline   0 Reply With Quote
Old Mar 25, 2008, 11:45 PM   #21
Buschmaster
Thread Starter
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
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"
What did I do wrong there? The only reason it would seem it isn't including it is because I have this:
Code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}
among other things in ClickControl.m and that one in particular should be exactly right. Even if I messed the click controls up I should be able to go into the simulator and rotate the device, correct? It does nothing when I tell it to rotate left or right as if I haven't accounted for it.
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com
Buschmaster is offline   0 Reply With Quote
Old Mar 26, 2008, 06:54 AM   #22
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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.
kainjow is offline   0 Reply With Quote
Old Mar 26, 2008, 02:32 PM   #23
Buschmaster
Thread Starter
macrumors 65816
 
Buschmaster's Avatar
 
Join Date: Feb 2006
Location: Minnesota
Send a message via AIM to Buschmaster
Quote:
Originally Posted by kainjow View Post
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.
I did that, and I think I utilized the rotation correctly. The Aspen Simulator doesn't rotate when I tell it to, yet, though. Here is all of my code in my ClickControl.m
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
I think my problem might be stemming from the fact that MyView draws the elements while ClickControl will be the class that watches for rotation and touches.

If there is other code that will help, let me know.
__________________
Mint Apps: Apps with beauty and simplicity: www.mintapps.com
Buschmaster is offline   0 Reply With Quote
Old Jul 8, 2008, 05:47 AM   #24
AvengerB
macrumors newbie
 
Join Date: Jul 2008
Same problem ! please help

Quote:
Originally Posted by Buschmaster View Post
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];
Off to read the suggested homework!

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
TestView.m

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
What am i Supposed to Add in the header file?
I have tried adding all the Header Files ..nothing seems to work. Please help me understand.
AvengerB is offline   0 Reply With Quote
Old Jul 8, 2008, 06:19 AM   #25
AvengerB
macrumors newbie
 
Join Date: Jul 2008
Solved!!! Finally after 14days!!

Quote:
Originally Posted by Buschmaster View Post
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];
Off to read the suggested homework!
Finally solved..
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.
AvengerB is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Mophie Juice Pack Plus (iPhone 5) Question - Mute Button Question JulesK iPhone Accessories 3 Mar 30, 2014 02:58 PM
Semi Newb Question Restore from iPhone 5 backup to iPhone 5S tgi iPhone 1 Sep 20, 2013 09:26 PM
Speck Candyshell for iPhone 5 question about size vs. iPhone 4 case iansilv iPhone Accessories 2 Oct 22, 2012 06:54 PM
General: Question for CPU gurus...(Iphone 5 question) Mercennarius Jailbreaks and iOS Hacks 0 Sep 20, 2012 08:46 PM
A Question about the UK, iPhone 4s & New iPhone Pre-Order Holmes093 iPhone 8 Aug 8, 2012 04:36 PM

Forum Jump

All times are GMT -5. The time now is 07:02 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC