Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-19-2011, 03:49 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default Program not adding subview on touch

Hi all,
I'm fairly new to the iPhone SDK. I'm working out a little test project which adds a small black dot every time you click the screen. First of all, is there a way I can add a dot without having to make a UIImageView?
I looked around the Internet, but I couldn't find anything. So the error-free code I came up with was this, in "FirstViewController.h":

Code:
@implementation FirstViewController

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	NSLog(@"Touches began!");
	UITouch *touch = [touches anyObject];
	CGPoint touchPoint = [touch locationInView:self.view];
	UIImage *blackDot = [UIImage imageNamed:@"Black Dot.png"];
	UIImageView *dotView = [[[UIImageView alloc] initWithImage:blackDot] initWithFrame:CGRectMake(touchPoint.x, touchPoint.y, 2.0, 2.0)];
	NSLog(@"Touch point frame: x: %g y: %g w: 2.0 h: 2.0", touchPoint.x, touchPoint.y);
	[self.view addSubview:dotView];
	[self.view bringSubviewToFront:dotView];
	NSLog(@"Added a dot.");
	[dotView release];

	
}
The output in the log is:
Code:
[Session started at 2011-02-19 15:36:12 -0600.]
2011-02-19 15:36:28.560 TabBarApp[56894:207] Touches began!
2011-02-19 15:36:28.587 TabBarApp[56894:207] Touch point frame: x: 256 y: 286 w: 2.0 h: 2.0
2011-02-19 15:36:28.588 TabBarApp[56894:207] Added a dot.
Thanks!
architectpianist is offline   Reply With Quote
Old 02-19-2011, 03:51 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default

What I can't figure out is, why doesn't the view controller add the subview?
architectpianist is offline   Reply With Quote
Old 02-19-2011, 03:55 PM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
[[[UIImageView alloc] initWithImage:blackDot] initWithFrame:CGRectMake(touchPoint.x, touchPoint.y, 2.0, 2.0)];
This is not a legitimate thing to do. There should only be 1 init. Either use the image, or use the frame. Not both.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-19-2011, 04:10 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default

Thanks, working great now!
Here's what I did:

Code:
	UIImageView *dotView = [[UIImageView alloc] initWithImage:blackDot];
	CGRect frame = {touchPoint.x, touchPoint.y, 2.0, 2.0};
	dotView.frame = frame;
The reason I did the two inits was because I had one tutorial telling me to initWithFrame, and one to initWithImage, and it wasn't working prior to the two inits anyway. But I guess I changed something along the way.

And thanks for the super-fast reply, too!
architectpianist is offline   Reply With Quote
Reply

Bookmarks

Tags
subview, touch, uiimageview, viewcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 381
14 members and 367 guests
cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:10 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0