Button not working in a table header

I have a custom view that implements table header. I have buttons in that view. The buttons used to work fine in SDK prior to 5. I moved directly to SDK 7, and the buttons stopped working. The action is not called anymore.
Any help will be appreciated.

Thanks

MAc Mini, Mac OS X (10.5.2)

Posted on Jun 22, 2008 1:54 PM

Reply
13 replies

Jun 25, 2008 1:48 PM in response to gkstuart

The view was programmatically created. The code was written before IB was added to the SDK. It used to work fine.
Here are some pieces of the code :
This is where the tableHeaderView is set.

CTopView *top = [[CTopView alloc] initWithWidth: self.view.bounds.size.width andCDef:chdef andIndex:self.cIndex ];
top.userInteractionEnabled = YES;
mListView.tableHeaderView = top;
[top release];


Where CTopView is a subclass of UIView. where the buttons are created as follows:


//add the previous arrow button
button = [[UIButton alloc] initWithFrame:CGRectZero];
[button setImage:leftArrow forState:UIControlStateNormal];
[button addTarget:self action:@selector(previousChannel:) forControlEvents:UIControlEventTouchUpInside];
button.tag = CHANNELLEFT_ARROWTAG;
[self addSubview:button];
[button release];


The sizes are calculated and set later in the - (void)layoutSubviews method.

So the selector (previousChannel) is never called.

Thanks

Jun 25, 2008 4:08 PM in response to rab101

Well, you have me stumped. I just replicated your code and it works fine for me:

- (void)doStuff:(id)sender
{
NSLog(@"Logo Touched!");
}
- (void)viewDidLoad {
// loadingView is a UIView that becomes the tableHeaderView
self.tableView.tableHeaderView = loadingView;
[loadingActivity startAnimating];

// setup a sample button (declared in header)
logoButton = [[UIButton alloc] initWithFrame:CGRectZero];
[logoButton setImage:[UIImage imageNamed:@"logo_webclip.png"] forState:UIControlStateNormal];
[logoButton addTarget:self action:@selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
logoButton.tag = 100;
[loadingView addSubview:logoButton];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

logoButton.frame = CGRectMake(50., 50., 64., 64.);
}


That calls my target everytime it's touched... are you remembering to call the superclass for each of your overrided methods in your custom UIView? The only difference between my quick test and yours is that I didn't subclass UIView -- I just used the UIView class directly.

Jun 26, 2008 3:24 PM in response to gkstuart

Thanks for your reply.

The UIView that I subclassed is simple, it has the init and layoutSubviews and both call their super methods.
When the button gets pressed/clicked the corresponding selector action does not get called. Instead the touch events on the TableView get called.

As I mentioned this used to work in all SDKs prior to 5.
The other difference that may cause this, is that I am using a regular UIViewController for the tableView. In later SDKs they have added UITableViewController. I am thinking to re-implement the app using UITableViewController, but I am not sure that this is really the cause of the problem.

Thanks

Jun 30, 2008 12:46 AM in response to rab101

As a follow up -- I tried setting the width and height of my UIButton explicitely to smaller than the image within it and the previous poster is correct. The simulator shows the entire image, but touches are only registered for the frame of the button. So if it is being set to (0,0) it would likely display but will not get touch events.

As a test, take your UIButton and set clipsToBounds = YES. Does your button image still appear? If not, that's likely your problem. As a second test, you can use this when you create the button:

button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,width,height)];

...and see if that works. Next step is to determine why the button is not having it's frame reset to proper dimensions in layoutSubviews. Are you setting the button's dimensions there?

Cheers,
George

Jun 30, 2008 1:54 AM in response to gorillafarmer

Yup, good point -- this is the proper way to do the button creation.

I was just following the OP's code from above. Manually setting the target/action/frame/etc. worked for my little test - but isn't the best way to do this. And I think I remember reading that clipsToBounds defaults to "NO". So I think you may have been correct earlier that this was the OP's problem.

rab101 - any luck?

Cheers,
George

Jul 2, 2008 3:25 PM in response to gkstuart

I tried both suggestions and none of them worked for me. So I tried a different solution by elimination.
I removed the implementation of the touchesBegan and touchesEnded from the UITableView and everything start working even the accessoryButtonTappedForRowWithIndexPath that used not to work.

So the UITableView that I am using is somehow getting all the touch events. Here is the implementation of touchesBegan:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
lastLocation = [touch locationInView:self];
[super touchesEnded:touches withEvent:event];
}


Now all buttons in the header and table action are working, but I lost the touch events that used to advance to the next screen.

Any ideas on how to solve this problem?

Thanks

Jul 2, 2008 10:58 PM in response to rab101

Two things:
1) UITableView is a subclass of UIScrollView. I have seen that UIScrollView "swallows" events -- probably due to it's desire to scroll content. I specifically saw this with the responder chain not propogating events +up the responder chain+. So a superview doesn't get the touchesBegan message if you use a default ScrollView. I suspect that these classes also implement a custom touchesBegan: method which tries to be intelligent about whether you are scrolling or tapping the screen. So if you implement any of the touches: methods in a subclass of UIScrollView you need to call their super class implementations.

2) It looks like you may have a typo. In your touchesBegan method you should call:

[super touchesBegan:touches withEvent:event];

You appear to be calling touchesEnded:.

Did that work for you?

Cheers,
George

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Button not working in a table header

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.