3

I want to add a view and a button programmatically like follows.

The problem is that the button does not react on clicking. I mean neither does it get highlighted or calls the selector.

The reason is that I want to implement a list row for a recording (a sound file). The list row should be selectable for drill-down and have a play button. So I got a RecordingView subclassing UIView that itself adds the button using a target from the constructor. See code below.

listrow

If anyone has a better approach to do this that could also be a solution.

@implementation MyViewController

- (IBAction) myAction { 
    RecordingView *recordingView = [[RecordingView alloc] initWithFrame:CGRectMake(30, 400, 130, 50)withTarget:self];
    [recordingView setUserInteractionEnabled:YES];
    [[self view] addSubview:recordingView];
}

@implementation RecordingView

- (id)initWithFrame:(CGRect)frame withTarget:(id) target
{
    self = [super initWithFrame:frame];

    UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(185, 5, 80, 40)];
    [playButton setTitle:@"Play" forState:UIControlStateNormal];
    [playButton setTitleColor:[UIColor darkTextColor]forState:UIControlStateNormal];
    // creating images here ...
    [playButton setBackgroundImage:imGray forState: UIControlStateNormal];
    [playButton setBackgroundImage:imRed forState: UIControlStateHighlighted];
    [playButton setEnabled:YES];
    [playButton setUserInteractionEnabled:YES];
    [playButton addTarget: target 
                   action: @selector(buttonClicked:) 
         forControlEvents: UIControlEventTouchDown];

    [self addSubview:playButton];

    return self;
}

When I add the button the same way, directly in the view controller .m-file, the button does react on clicking. So there is something about the RecordingView. What do I need to do different here?

Also, are there any better ways to provide the target and selector for the touch event?

3
  • Is that your actual code? Where do you declare or populate the two UIImage variables (imGrey and imRed)? You're in an init method, so they can't be ivars? In relation to your question, you say you want a list of recordings - are you after a table view? Could your recording view be a table cell subclass instead? Commented Feb 24, 2012 at 8:02
  • True, I omitted the code for creation of images. This is my actual code but I remove stuff when posting, for simplicity. I will clarify this. Yes, the recording view could be a table cell instead. I am new to building iOS UI from code. This is one of my first, so any directions forward are welcome. :) Commented Feb 24, 2012 at 8:57
  • @jrturton: Yes, I am planning to use this code in a UITableViewDelegate, in the method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. ... when I get it to work, that is. ^^ Commented Feb 24, 2012 at 17:31

1 Answer 1

5

You may simply need to set userInteractionEnabled to YES on your RecordingView.

Another problem is that you are creating the RecordingView with a frame width of 130, but you are setting the X-axis origin of playButton to 185. This means playButton is entirely outside of the bounds of its superview. The default value for clipsToBounds is NO, so the button is drawn anyway. But touch events will never reach the button, because they are rejected when the system hit-tests the RecordingView.

This is from the hitTest:withEvent: documentation in UIView Class Reference:

Points that lie outside the receiver’s bounds are never reported as hits, even if they actually lie within one of the receiver’s subviews. This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.

You need to either make RecordingView's frame wider, or move playButton to be inside the bounds of its superview.

Sign up to request clarification or add additional context in comments.

1 Comment

it was the width, thanx so much

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.