UIViewController and UIScrollView - objective-c

I have a custom UIViewController class whose view (hooked up in IB) is a UIScrollView. I want to know when the UIScrollView gets "touch up inside".
I overrode the
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
method but it doesn't get called. What am I doing wrong?

Did you override touchesBegan:: in the UIViewController or the UIScrollView? If you did it on the UIViewController, it needs to be in the UIScrollView (or whatever it contains).
The UIScrollView is intercepting the touch events before it "bubbles" to your UIViewController.

like ryan said, you need to override your touchesBegan:: method in the UIScrollView. you may need to create a custom scrollView for this purpose and set the scrollView you use (in IB) to that class. also, be sure to set the scrollView delegate to your view controller. that way, when touches are intercepted, it knows where to send them

Related

How to Stop a UIScrollView from Swallowing Touches

I have UIScrollView that has many subviews. When I scroll, I want to tap on a subview which I want to be dragged. Is there a possible way to make the UIScrollView stop from swallowing touches? Or is it possible to start new touch when you cancel the scrolling (like what it scrolls and I tapped on it, the subview will be tapped as well so I can drag it out)?
Subclass UIScrollView and override the - (BOOL)touchesShouldCancelInContentView:(UIView *)view method. Here's an example to allow uibutton touches to pass through:
#import "scrollViewWithButtons.h"
#implementation scrollViewWithButtons
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return ![view isKindOfClass:[UISlider class]];
}
#end

Adding touch-area to UIView (iOS5)

I have a MainView with MainViewController. Now I want to add a specific area where I want to register touches (painting in a specific area). How could I do this?
I thought about adding a sub-view with its own sub-viewcontroller, but this guy tells this is not a good approach.
The post you linked to is partially out of date because it was written before Apple introduced support for View Controller Containment in iOS 5.
That said, it's your choice whether:
the subview is managed by its own view controller or
you use the MainViewController directly to respond to touches in the subview or
you create a UIView subclass that interprets touches on itself without the help of a view controller.
Add a custom view as a property, called touchArea
-(void) touchesBegan/Moved/Ended (NSSet *)touches withEvent:(UIEvent *)event {
CGPoint location = [[touches anyObject] locationInView:self.view];
if (CGRectContainsPoint(touchArea.frame, location))
//code
}
}

Affecting UIViewController from a child UIView

My question might sound rather strange, but still I didn't find any reference and I really need help.
so my point is, I have a ViewController which has a collection of UIViews, to be specific a subclass of UIView with
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
overridden
from this event i want to load a specific data (according on the UIView touched) on the UIViewController's child UIWebView
Is there a way to address parent ViewControllers children from the UIView that receives the action
Or am I looking into a completely wrong direction.
Any hel will be greatly appreciated, thanks in advance.
You can use delegate for example:
#protocol someProtocol
- (void)recivedTouch:(UITouch *)touch fromUIView:(UIView *)uiView;
in you view:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
[self.delegate recivedTouch:touch fromUIView:self];
}
View controller implemed method from delegate:
- (void)recivedTouch:(UITouch *)touch fromUIView:(UIView *)uiView
{
if (uiView == self.someView)
{
// do some stuff with uiWebView
}
}
try to look at it differently. Your touchesBegan shouldn't do anything which is not connected with the view. Your view is just a UI component, it shouldn't control application flow (data loading). That's the job of the view controller.
Look how UIButton is implemented. The button does not do anything when clicked. The controller has to tell it "this is the method that should be called when the user clicks you"
There are two ways how to tell the view what it should do for a specific action - creating a delegate or passing a selector that should be called.
Consider also extending UIControl, which has most of the actions already implemented.
Alternatively use NSNotificationCenter: Send and receive messages through NSNotificationCenter in Objective-C?
Send a message from your view, hook the message from your controller. Notificationas can get a bit messy though, the delegate is probably a cleaner solution

event touchesForView: returns null in super view

I have a subclass of UIView called BigView that overrides touchesMoved (among other things) like so:
#implementation BigView
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet * viewTouches = [event touchesForView:self];
NSLog(#"set of touches: %#", viewTouches);
}
#end
My BigView instance also has a subview (regular UIView instance) - when I touch inside that subview, the above touchesMoved method gets called, but viewTouches comes up null. The subView doesn't override any of the event handling methods (touchesBegan, touchesMoved, etc). I would expect the touch count for a view to include all of the touches inside its subviews, but it doesn't seem to be working that way. Am I doing something wrong in the code, or is this the way it should work and I don't understand why? (If the latter, why is this better?)
Thank you!

UIButton as part of UITableViewCell subview - make it work

I'm having some class which is a subclass of UITableViewController.
on one of the TableView's cell I'm adding a view that holds a UIButton as subview
(i.e. [someParentView addsubview:button])
I'm adding the view to cell like this:
[cell.contentView addSubview:someParentView]
I've set the UserInteractionEnabled both for the button and for the view holding it ("someParentView") to YES
but still when I tap it, nothing seems to be happening.
any idea?
Thanks
What i want to accomplish in short: i want to make a tableview that shows some views.
each view contains some subviews, and in one of those cases there is a uibutton as a subview.
i want to have this button to trigger event by user's tap, as any other uibutton, so some method will be launched.
to do that, i made a class which subclasses UITableViewController, and for each cell i added a view using [cell.contentView someView], as i wrote. i disabled selection from all using the [self.tableview setAllowSelection:NO] and for each sell made selection style as NONE.
as said, i also set the view and the uibutton UserInteractionEnabled property to YES.
anything i'm missing?
Been solved!
I have used the method:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
of UIView class in my subclass and returned the button as the returned object.
(of course, needed to test point if in button area).