views:

189

answers:

2

guys, i want to click my uiButton after it's added to UIImageVIew, but it doesn't work. this is my code :

UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];

btnDetail.frame = CGRectMake(0,0,100,100);
[btnDetail addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents];

[self.imageView addSubview:btnDetail];

the button can't click when i add it to UIImageVIew, but if i don't add it to UIImageView, it works properly. please somebody help me

+1  A: 

Use of this code will surely work.. but i havent tried it yet

UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];

btnDetail.frame = CGRectMake(0,0,100,100);
[btnDetail addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents];

[self.imageView addSubview:btnDetail];
[self.imageView bringSubviewToFront:btnDetail];
[self.imageView setUserInteractionEnabled:YES];

HAppy Coding...

Suriya
i've try your code, but nothing happen, i still cant click it.for more information : before i add the UIButton, i've been add another UIImage to this UiVIewImage, is there affect.??
Imam Arief W
Please check out the new code.. I have implemented it and tested. Its working fine now.. Actualy the userinteraction of imageview is bydefault off so you werent able to click on anything added to it. So I have set its usre interaction as enabled. Now working fine
Suriya
thnx guys it works perfectly, i can't believe my mistake is just put [self.imageView setUserInteractionEnabled:YES]; before i add the subview, sure it won't works,, hha ridiclous,, btw thank you so much guys...
Imam Arief W
always welcome dude....
Suriya
+1  A: 

UIImageView has userInteractionProperty set to NO by default so it does not handle touches and does not propagate them to its subviews. Try to set it to YES:

self.imageView.userInteractionEnabled = YES;
Vladimir
i've try, but still doesn't work
Imam Arief W
I've just tried that in a sample project - it works perfectly for me...
Vladimir
thnx guys, it works perfectly fo me too..
Imam Arief W