![](/img/trans.png)
[英]Why is my "touch up inside" event for my UIButton not being called?
[英]UIButton touch up inside event only fired if done in title
我正在自定義具有幾個子視圖的UITableViewCell
:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_mainView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)];
_hiddenOptionsView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)];
_menuView = [[CellMenuView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 20.0f)];
[self addSubview:_menuView];
[self addSubview:_hiddenOptionsView];
[self addSubview:_mainView];
}
return self;
}
CellMenuView
類是一個UIView
子類,它具有兩個UIButtons
並在初始化時設置了對應的目標動作:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIImageView *background = [[UIImageView alloc] initWithFrame:frame];
[background setImage:[UIImage imageNamed:@"cell_menu_bg.png"]];
[self addSubview:background];
CGFloat buttonX = frame.origin.x;
CGFloat buttonY = frame.origin.y + 3.0f;
CGFloat buttonWidth = frame.size.width / 2.0f;
CGFloat buttonHeight = 10.0f;
_editButton = [UIButton buttonWithType:UIButtonTypeCustom];
_editButton.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
_editButton.backgroundColor = [UIColor clearColor];
_editButton.titleLabel.shadowColor = [UIColor blackColor];
_editButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.5f);
_editButton.titleLabel.font = [UIFont boldSystemFontOfSize:22.0f];
[_editButton setTitle:@"Edit" forState:UIControlStateNormal];
[_editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_editButton addTarget:self action:@selector(editButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_editButton];
_fireButton = [UIButton buttonWithType:UIButtonTypeCustom];
_fireButton.frame = CGRectMake(buttonWidth, buttonY, buttonWidth, buttonHeight);
_fireButton.backgroundColor = [UIColor clearColor];
_fireButton.titleLabel.shadowColor = [UIColor blackColor];
_fireButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.5f);
_fireButton.titleLabel.font = [UIFont boldSystemFontOfSize:22.0f];
[_fireButton setTitle:@"Fire" forState:UIControlStateNormal];
[_fireButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_fireButton addTarget:self action:@selector(fireButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_fireButton];
return self;
}
}
我已經實現了目標行動的方法,但他們沒有被執行。 相反,每次我按下按鈕之一時, didSelectRowAtIndexPath:
優先於按鈕內的didSelectRowAtIndexPath:
事件。
我能夠在按鈕上觸發事件的唯一方法是確保觸摸UIButton's
標題中的字母之一(當然是使用模擬器)。
我必須說, _menuView
隱藏在其他子視圖的后面,並在按下單元格中的自定義附件按鈕時顯示在單元格下方。 修改Y原點會顯示它,而再次將其設置為0.0f
消失。
我認為這可能與視圖層次結構有關,因為過去通過將按鈕直接添加到單元格沒有任何問題。 但是我只是在猜測。
如何使按鈕上的事件優先於didSelectRowAtIndexPath:
方法?
謝謝!
實行
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
然后為您不想選擇的indexPath返回nil; 這樣可以防止在該單元格上調用didSelectRowAtIndexPath
,並且(希望)將調用您的自定義操作。
更新:我自己實現了此操作(在按鈕上添加了邊框),這就是我所擁有的。
實現此目的的代碼(與您的不同)如下:
CellMenuView.m
self.backgroundColor = [UIColor blackColor];
_editButton.layer.borderColor = [UIColor whiteColor].CGColor;
_editButton.layer.borderWidth = 2;
_fireButton.layer.borderColor = [UIColor whiteColor].CGColor;
_fireButton.layer.borderWidth = 2;
UITableViewCell子類
// Change in height to make it taller
_menuView = [[TestBtn alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 320.0f, 60.0f)];
UITableViewController子類
self.tableView.rowHeight = 100;
經過測試,我的工作正常。 如果我在白方塊內單擊, didSelectRowAtIndexPath
常規執行按鈕操作,並且不會調用didSelectRowAtIndexPath
。 僅當我在按鈕外部單擊時才調用didSelectRowAtIndexPath
。
我認為這里的問題可能是您的按鈕高度/框架以及行高。 在按鈕上添加邊框,以檢查其可單擊區域,並增加按鈕的高度以增加可單擊區域。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.