views:

584

answers:

3

I have a really simple app, and I'm stuck just in the beginning. Let me post the code right here.

TapStackView.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface TapStackView : UIView
{
    //Layers.
    UIImageView *backgroundImageView;
    UIImageView *powerOn;
}
@property (nonatomic, retain) UIImageView *backgroundImageView;
@property (nonatomic, retain) UIImageView *powerOn;
@end

TapStackView.m

#import "TapStackView.h"

@implementation TapStackView
@synthesize backgroundImageView, powerOn;

//Initialize,
-(id)initWithFrame: (CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        //Set up layers.
        self.backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"layout.png"]];

        self.powerOn = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"powerOn.png"]];     
        self.powerOn.center = CGPointMake(159 + powerOn.frame.size.width/2, 26 + powerOn.frame.size.height/2);

        //Attach to the view.
        [self addSubview:self.backgroundImageView];
        [self addSubview:powerOn];      

        NSLog(@"initWithFrame - powerOn.center is %f, %f", self.powerOn.center.x, self.powerOn.center.y);   

    }
    return self;
}

//Finger touched.
-(void)touchesBegan: (NSSet*)touches withEvent: (UIEvent*)event
{
    NSLog(@"-----");
    NSLog(@"touchesBegan - self.powerOn.center is %f, %f", self.powerOn.center.x, self.powerOn.center.y);   
}

@end

Console

[Session started at 2010-01-01 21:39:42 +0100.]
2010-01-01 21:39:44.177 TapStack[6261:20b] initWithFrame - powerOn.center is 186.000000, 53.000000
2010-01-01 21:39:45.149 TapStack[6261:20b] -----
2010-01-01 21:39:45.150 TapStack[6261:20b] touchesBegan - self.powerOn.center is 0.000000, 0.000000
2010-01-01 21:39:45.978 TapStack[6261:20b] -----
2010-01-01 21:39:45.979 TapStack[6261:20b] touchesBegan - self.powerOn.center is 0.000000, 0.000000
2010-01-01 21:39:48.914 TapStack[6261:20b] -----
2010-01-01 21:39:48.915 TapStack[6261:20b] touchesBegan - self.powerOn.center is 0.000000, 0.000000
2010-01-01 21:39:50.011 TapStack[6261:20b] -----
2010-01-01 21:39:50.012 TapStack[6261:20b] touchesBegan - self.powerOn.center is 0.000000, 0.000000

I just cannot imagine why can't I see that powerOn property inside the touchesBegan method. Any ideas?

+1  A: 

You need to set .userInteractionEnabled on your view:

myView.userInteractionEnabled = YES;

From the docs: New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIImageView_Class/Reference/Reference.html

I also think you probably need to subclass UIImageView and use that for your member variable, instead of a straight UIImageView. If you want to detect touches on the UIImageView, you should set up the touchesBegan delegate method for the UIImageView subclass, not your UIView subclass.

Though, you could detect touches in your UIView and then use CGRectContains to see if they are touching the appropriate UIImageView

Andrew Johnson
I have tried userInteractionEnabled with the same result.Subclassing sounds promising.
Geri
A: 

Note: I'm assuming that the UIImageView subviews display properly and that they do have non-zero centers.

It's rare but sometimes the dot notation does not resolve properly. Try the old nested style of reference.

NSLog(@"touchesBegan - self.powerOn.center is %@",NSStringFromCGPoint([[self powerOn] center]);

If this works, then you probably have a linking problem. Check you build results for any warnings related to UIImageViews. Sometimes the linker seems to loose track of some of the nested classes. A clean all usually fixes the problem.

TechZen
Gonna try in a moment.
Geri
+1  A: 

I can't reproduce this. How are you adding TapStackView to its superview? From a Window-based template, I just added this to applicationDidFinishLaunching:

    [window addSubview:[[[TapStackView alloc] initWithFrame:[window bounds]] autorelease]];

It seems to work as you would expect:

2010-01-01 17:23:00.873 Untitled[3708:207] initWithFrame - powerOn.center is 215.000000, 90.000000
2010-01-01 17:23:02.940 Untitled[3708:207] -----
2010-01-01 17:23:02.941 Untitled[3708:207] touchesBegan - self.powerOn.center is 215.000000, 90.000000
2010-01-01 17:23:11.199 Untitled[3708:207] -----
2010-01-01 17:23:11.200 Untitled[3708:207] touchesBegan - self.powerOn.center is 215.000000, 90.000000
2010-01-01 17:23:11.663 Untitled[3708:207] -----
2010-01-01 17:23:11.664 Untitled[3708:207] touchesBegan - self.powerOn.center is 215.000000, 90.000000
2010-01-01 17:23:11.665 Untitled[3708:207] -----
2010-01-01 17:23:11.665 Untitled[3708:207] touchesBegan - self.powerOn.center is 215.000000, 90.000000

BTW, you're leaking both backgroundImageView and powerOn.

Rob Napier
Ya, that seems ok.Maybe the problem is "above" TapStackView.
Geri
Hey thanks for the proving.Gave me courage enough to get rid of the unnecessary viewController class.Just work as it has to be.Again, thanks for testing.
Geri