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 10-20-2010, 10:29 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 20
robamacaf is on a distinguished road
Default How to Programmatically create TouchBackground event

I am not using IB because I have the view being setup dynamically and I want to make it that when someone clicks on the background of the view it will fire an event to hide the keyboard. I have the code to hide the keyboard already because I've used it to do the same thing through IB before. but for the life of me I can't figure out how to set up the TouchDown event on the view programmatically.

Thanks in advance!
Rob
robamacaf is offline   Reply With Quote
Old 10-20-2010, 02:41 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

implement the touchesBegan method
smithdale87 is offline   Reply With Quote
Old 10-20-2010, 08:03 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 20
robamacaf is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
implement the touchesBegan method
Mind adding in some basic code I can go off of? I'm still pretty new at this and learn best when I have something to compare it to

thanks
robamacaf is offline   Reply With Quote
Old 10-20-2010, 10:59 PM   #4 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 324
mariano_donati is on a distinguished road
Default

In your view's implementation file, write this code:

Code:
-(void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    // Your action code here
}
Replace the comment with your code.

Regards.
mariano_donati is offline   Reply With Quote
Old 10-21-2010, 08:00 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 20
robamacaf is on a distinguished road
Default

Quote:
Originally Posted by mariano_donati View Post
In your view's implementation file, write this code:

Code:
-(void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    // Your action code here
}
Replace the comment with your code.

Regards.
Thanks for the reply but that didn't seem to work and I think it's because I want it to work on a scroll view and I'm creating the scroll view programmatically...i might be wrong on that assumption. I have two files, NewProject.m and NewProject.h. In NewProject.M I create a scrollView where I have it lay out all the content based on what type of project the user wants to view.

Code:
	scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
	scrollView.contentSize = CGSizeMake(320,860);
	scrollView.tag = 1234;
	scrollView.pagingEnabled = FALSE;
	scrollView.scrollEnabled = TRUE;
	scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
	[self.view addSubview:scrollView];
I need to get the background touched event to fire when that scrollView is clicked. I tried putting in the code you told me to in the NewProject.m but it didn't do anything (I added a NSLog(@"Event Fired"); into your code just to try it and it never fired.)

Thanks again,
Rob
robamacaf is offline   Reply With Quote
Old 10-21-2010, 08:17 AM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 20
robamacaf is on a distinguished road
Default

I got it! I was googling the methods that you mentioned and came upon this. So I changed the code above and added the two lines at the bottom and it worked!!!!

Code:
	scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
	scrollView.contentSize = CGSizeMake(320,860);
	scrollView.tag = 1234;
	scrollView.pagingEnabled = FALSE;
	scrollView.scrollEnabled = TRUE;
	scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
	[self.view addSubview:scrollView];
	UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
	[scrollView addGestureRecognizer:singleTap];
Then I also added the method

Code:
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer \
{
    NSLog(@"Single Tap");
}


Thanks for putting me on the right track! You guys rock!

Rob
robamacaf is offline   Reply With Quote
Old 10-21-2010, 09:28 AM   #7 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

welcome
smithdale87 is offline   Reply With Quote
Old 10-21-2010, 09:29 AM   #8 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 324
mariano_donati is on a distinguished road
Default

Yes indeed. You scroll view occupies the whole display. Only the top-most UIView will receive touches.
UIGestureRecognizer is for iOS 3.2 and later. If you're targetting at this it should be fine, but if you want to make your code to work on iOS 3.0 as well, you'll need to subclass UIScrollView and implement touchesBegan method like it was mentioned.
mariano_donati is offline   Reply With Quote
Old 10-21-2010, 10:35 AM   #9 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 30
vsching is on a distinguished road
Default check the documentation

u should learn to check the documentation under help -> documentation. I suggest u can ask for some help, get the method name if really no idea, and then try figure it out with the help of the documentation. Don't just based on google and copy paste. That won't do you any good, in long term.
vsching is offline   Reply With Quote
Reply

Bookmarks

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: 327
7 members and 320 guests
Desert Diva, dre, hain, mottdog, oceanlablight, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

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