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.
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.
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.)
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!!!!
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.
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.