简体   繁体   中英

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];

I touch up inside (test ) button. but is not working . why??

Maybe you mixed buttonPressed: with buttonPressed .

If you implement a method like

- (void)buttonPressed
{
}

You need use

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

Note : Pay attention on the colon .

But depending on what you have done, you need implement a method like

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

for a cleaner solution you can try HMUIButtonOnTouch

source: https://github.com/HelloMihai/HMUIButtonOnTouch

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM