Alexidze
Alexidze

Reputation: 183

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.

Upvotes: 0

Views: 993

Answers (3)

Sulthan
Sulthan

Reputation: 130200

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.

Upvotes: 1

amleszk
amleszk

Reputation: 6290

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

Upvotes: 0

Michal Zaborowski
Michal Zaborowski

Reputation: 5099

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

Upvotes: 2

Related Questions