0
votes

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.

3
keep the MVC pattern in mind, views are only responsible for displaying data, user actions should always be handled in the viewcontrollerFelix

3 Answers

2
votes

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
    }
}
1
votes

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.

0
votes

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