簡體   English   中英

UIButton Touch事件無效

[英]UIButton Touch event is not working

menuView= [[UIImageView alloc] initWithFrame:CGRectMake(0,411, 320,49)];

UIButton *test = [UIButton buttonWithType:UIButtonTypeCustom ];
test.frame = CGRectMake(0, 0, 80, 49);

[test addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

[menuView addSubview:test];

[self.view addSubview:menuView];

我在里面(測試)按鈕觸摸。 但是沒有用。 為什么??

也許你混合了buttonPressed: with buttonPressed

如果你實現了像這樣的方法

- (void)buttonPressed
{
}

你需要使用

[test addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

注意 :注意結腸

但是根據你所做的,你需要實現一個類似的方法

- (void)buttonPressed:(id)sender
{
}

為了更清潔的解決方案,您可以嘗試HMUIButtonOnTouch

來源: https//github.com/HelloMihai/HMUIButtonOnTouch

[self.myButton onTouchInside:^{
    NSLog(@"button tap");
}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM