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
Related
I have a normal app. Yes this will not work anywhere in it. If I place this in a ViewController, it will not work, if I place this in a UIView subclass, likewise, it refuses to work. What am I doing wrong?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Please Work");
}
Make sure the view has userInteractionEnabled=YES. If a parent view has userInteractionEnabled=NO, the touches will be lost too. If that doesn't help, post code.
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
}
}
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!
I have a bunch of UIViews stacked one upon the other(not nested). I want them all to react to touch, but it seems that the topmost view obscures the views beneath it, preventing them from receiving touch events.
At first i thought i’d catch all touch events with the topmost view, and then manually call
hitTest, or pointInside methods on all the underlying views, but i found out that both methods are somehow private(could it be?) and cannot be accessed.
Any ideas how to pull it off?
You can check if the touch is for your topmost view. If it doesn't you can call the same method of your superview. Something like [self.superview sameMethod:sameParameter].
Your topmost view has a method
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
Inside that method you are doing your logic right?
Inside the method can't you check if the touch received is at your topmost view with
UITouch *touch = [touches anyObject];
[touch locationInView:self];
And if it doesn't you pass it to the superView's same method using
[self.superview touchesEnded:touches withEvent:event];
Touches are sent to a single view. That view can then optionally pass them up the responder chain. If you want to handle touches to a collection of views you should have them forward those events up to the next responder and have a common parent of all of them (or their view controller since the controller is also part of the responder chain) handle those touches.
https://developer.apple.com/library/mac/documentation/General/Devpedia-CocoaApp-MOSX/Responder.html
I want to personalize my UITableView changing background when user tap on a specific cell.
I've a dedicated ViewController for each cell and if I implement touchesBegan method in this viewController i can change my cell background without any problem. The problem is that the method "didSelectRowAtIndexPath" of the UITableView is no longer called. How can I call it manually? I am in another viewController and I have no access to that method. or how can propagate to the touch tableView?
Thanks a lot
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//do your operations
//send it to super class
[super touchesBegan:touch withEvent:event];
}
[super touchesBegan:touch withEvent:event];
Are you sending your action to super class? If not then add this line.