How can I get the ViewController instance starting from subView

I've an object of UIImageCustom that extend UIImageView, and in a method this class I want to get instance the ViewController, where my object UICustom is added.

In touchesBegan method of my class UIImageCustom, when I touch in UIImage, I want add other UIImage in my ViewController:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    {
        UIImageView* newImageView=[[UIImageView alloc] initWithFrame:CGRectMake(60, 200, 200, 200)];

        //Here I need add newImageView in my ViewController
        [??? addSubview:newImageView];
    }

Someone can help me?

Answers


There are a bunch of ways you could do this (set a custom property in your UIImageCustom class, just have UIImageCustom add this new image view to its own superview (e.g. [self.superview addSubview:newImageView]). But these approaches seem conceptually flawed. A view shouldn't be adding anything other than to itself (and if you turned off clipping, adding this new image view as a subview of the UIImageCustom, itself, might be another approach, certainly less offensive than the previous two I alluded to).

But I might suggest a couple of other approaches:

  1. If your goal is simply to avoid duplication of your touches code, I might put it in a gesture recognizer subclass, rather than attaching it to some random UIImageView subclass. Gesture recognizers is the right place to put this sort of touch-related code. See the Gesture Recognizers section of the Event Handling Guide for iOS.

  2. Or if you really need to marry this gesture with with a bunch of image views, I might use a custom container view controller to mediate this interaction between gestures and image views. See Creating Custom Container View Controllers in the View Controller Programming Guide for more information.

    But you could wrap the touches code and the handling of these two image views all within a view controller, and then use view controller containment to add that to an existing view controller.

We don't know enough about the particular design to suggest one over another, but those are a couple of ideas to pursue. Most likely, the custom gesture recognizer approach should be more than sufficient.


You'll have to add a custom property to your UIImageCustom class that contains a reference to the owner view controller if you want to do what you're saying you want to do. But there's a better way.

What I suggest you should be doing is implementing touchesBegan in the view controller that owns the view, and performing any custom logic there.


Create a category like this

@interface UIViewController (TOP_MODAL)
-(UIViewController*) topModalController;
@end

@implementation UIViewController (TOP_MODAL)
-(UIViewController*) topModalController{
    if (self.presentedViewController == nil){
        return self;
    }
    return self.presentedViewController.topModalController;
}

@end

then

AppDelegate* delegate =  (AppDelegate*)[[UIApplication sharedApplication] delegate];
UIViewController* viewController = [delegate.window.rootViewController topModalController];
...
[viewController.view addSubview:newImageView];

Need Your Help

debugging flash authored content with the flex debugger

flex flash debugging

I am working with a project that is developed in the flash environment, but I love the flex/eclipse debugger in that i can throw a breakpoint in and inspect the vars that are present. Is there a wa...

Java contains doesn't work as expected because "someString" != "someString"

java string list equals contains

I want check whether a String value val is contained within a List of Strings lets call it stringList.