Question

I have a subclassed UIView called TargetView that contains several CGPaths. When users click on any of the CGPaths (in UIView's touchesBegan) I would like to make changes to the parent view controller. Here is code from TargetView (UIView)

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    CGPoint tap = [[touches anyObject] locationInView:self];

    if(CGPathContainsPoint(region, NULL, tap, NO)){
        // ...do something to the parent view controller
    }
}

How might I do this? Thanks!

No correct solution

OTHER TIPS

I would suggest that you set the parent view controller as a delegate to the child view controller. Then when touches are detected in the child view controller, you can call the delegate to respond. This way, your child view controller will only have a weak reference to the parent.

if (CGPathContainsPoint(region, NULL, tap, NO)) {
    [self.delegate userTappedPoint:tap];
}

You need to pass a reference to the parent viewController to the UIView on allocation, and store this in a property on the UIView Then you have a reference to the parent and you can use this to call methods/set properties on that parent.

Use protocol and set parent view controller as delegate for your UIView.

In your UIView subclass .h file:

@protocol YourClassProtocolName <NSObject>

@optional
- (void)methodThatNeedsToBeTriggered;

@end

@interface YourClass : UIView

...

@property(weak) id<YourClassProtocolName> delegate;

@end

In .m file:

@interface YourClass () <YourClassProtocolName>
@end

@implementation YourClass
...

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    CGPoint tap = [[touches anyObject] locationInView:self];

    if(CGPathContainsPoint(region, NULL, tap, NO)){
        if (_delegate && [_delegate respondsToSelector:@selector(methodThatNeedsToBeTriggered)]) {
            [_delegate methodThatNeedsToBeTriggered];
        }
    }
}
@end

And now set needed UIViewController as delegate for this new protocol and implement methodThatNeedsToBeTriggered in it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top