tableBooterView上的UIButtons没有响应事件 [英] UIButtons on tableFooterView not responding to events

查看:86
本文介绍了tableBooterView上的UIButtons没有响应事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功添加了一些UIButtons到自定义tableFooterView。问题是按钮事件不被调用。此外,相对于UIControlStateHighlighted的图像不会出现。有任何想法吗?
我试过了所有我想到的,但我找不到的伎俩。我在这里报告UITableViewController的有趣的部分:

   - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection: (NSInteger)section 
{
CGRect footerFrame = CGRectMake(0.0,0.0,480,190);
_footerView = [[UIImageView alloc] initWithFrame:footerFrame];
_footerView.image = [UIImage imageNamed:@tableviewfooter.png];
_footerView.contentMode = UIViewContentModeTopLeft;

UIButton * addExam = [self createButtonWithFrame:CGRectMake(10,30,460,58)Target:self Selector:@selector(addNewItem :) Image:@additem.pngImagePressed:@ .png];
[_footerView addSubview:addExam];

return _footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 190;
}


- (void)addNewItem:(id)sender
{
//这个get从不调用
NSLog(@ Pressed);
}


- (UIButton *)createButtonWithFrame:(CGRect)frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed :( NSString *)imagePressed
{
UIButton * button = [[UIButton alloc] initWithFrame:frame];

UIImage * newImage = [UIImage imageNamed:image];
[button setBackgroundImage:newImage forState:UIControlStateNormal];

UIImage * newPressedImage = [UIImage imageNamed:imagePressed];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

返回按钮;
}

感谢您的帮助!

解决方案

我不认为 UIImageView 被设计为用作其他视图的容器,特别是 UIButtons 。例如, UIImageView s在创建时会有 userInteractionsEnabled 属性设置为 false



我将创建一个简单的 UIView 您的页脚视图,然后直接添加 UIButton UIImageView


I've successfully added some UIButtons to a custom tableFooterView. The problem is that the button events does not get called. Also, the image relative to the UIControlStateHighlighted doesn't come up. Any ideas? I've tried all I could think of, but I can't find the trick. I'm reporting here the interesting part of the UITableViewController:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection: (NSInteger)section 
{
    CGRect footerFrame = CGRectMake(0.0, 0.0, 480, 190);
    _footerView = [[UIImageView alloc] initWithFrame: footerFrame];
    _footerView.image = [UIImage imageNamed:@"tableviewfooter.png"];
    _footerView.contentMode = UIViewContentModeTopLeft;

    UIButton *addExam = [self createButtonWithFrame: CGRectMake(10, 30, 460, 58) Target: self Selector: @selector(addNewItem:) Image: @"additem.png" ImagePressed: @"additem_pressed.png"];
    [_footerView addSubview:addExam];

    return _footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger) section
{
    return 190;
}


- (void) addNewItem:(id)sender
{
    // This get's never called
    NSLog(@"Pressed");
}


- (UIButton*) createButtonWithFrame: (CGRect) frame Target:(id)target Selector:(SEL)selector Image:(NSString *)image ImagePressed:(NSString *)imagePressed
{
    UIButton *button = [[UIButton alloc] initWithFrame:frame];

    UIImage *newImage = [UIImage imageNamed: image];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];

    UIImage *newPressedImage = [UIImage imageNamed: imagePressed];
    [button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    return button;
}

Thanks for your help!

解决方案

I do not think UIImageView was designed to work well as a container for other views, especially UIButtons. For one, UIImageViews, when created, have their userInteractionsEnabled property set to false by default.

I would just create a simple UIView as your footer view and then add both the UIButton and the UIImageView directly to it.

这篇关于tableBooterView上的UIButtons没有响应事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆