How can I put a info button in the imageview frame reserved in uitableViewcell?
Thanks in advance!
All you need to do is create the button and add it to the cell:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton setFrame:CGRectMake(10,10,infoButton.frame.size.width, infoButton.frame.size.height)];
[infoButton addTarget:self action:#selector(infoButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:infoButton];
Edit---
As you want to use the imageView property of the cell, you are required to provide your own image and you need to set a gesture recogniser so that you know when the imageView has been tapped.
[cell.imageView setImage:[UIImage imageNamed:#"info-button.png"]];
[cell.imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(infoImageTapped:)];
[tap setNumberOfTapsRequired:1];
[cell.imageView setGestureRecognizers:[NSArray arrayWithObject:tap]];
[tap release];
- (void) infoImageTapped:(UITapGestureRecognizer *) tap {
//show info view.
}
Related
I am creating a barbuttonitem with the following code and i am adding a gesture recognizer to the navigation bar. So i need to know when the user tapped on the left or right of the barbuttonitem by 25 pixels. Is this possible using the gesture recognizer? Any help appreciated...
UIButton *searchButton = [self createSearchButton];
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGestureForTopLeftNotificationBarButtonItem:)];
[self.navigationController.navigationBar addGestureRecognizer:gr];
- (UIButton *) createSearchButton {
UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchButton setImage:[UIImage sdk_imageName:#"Search_header_black"]
forState:UIControlStateNormal];
[searchButton addTarget:self action:#selector(searchBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
searchButton.size = CGSizeMake(32, 30);
searchButton.imageEdgeInsets = UIEdgeInsetsMake( 0, 8, 0, 0);
return searchButton;
}
I know how to create popovers if my button added to story board, but how can I create popover if my button created via code.
UIButtonS *button = [UIButtonS buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(siteButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[button setTitle:string1 forState:UIControlStateNormal];
button.frame = CGRectMake(XLocatioan, YLocation, 90, 30);
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[button addGestureRecognizer:longPress];
[self.view addSubview:button];
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
}
else if (sender.state == UIGestureRecognizerStateBegan){
//create popover for button
}
}
You are already doing it right, but you are overthinking. There is no need to check the state of the gesture recognizer. If the target function has been triggered, it means that the user has done a long press. Also, note that not all of the values of the property state may be supported. As the documentation says: Some of these states are not applicable to discrete gestures.
So your code should look like this (unless you want to implement dragging or something similar):
UIButtonS *button = [UIButtonS buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(siteButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[button setTitle:string1 forState:UIControlStateNormal];
button.frame = CGRectMake(XLocatioan, YLocation, 90, 30);
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[button addGestureRecognizer:longPress];
[self.view addSubview:button];
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
//create popover for button
}
If your target is iOS 6+ you should use a UIPopoverController to create the popover, otherwise use UIAlertView.
I try to implement tapping on a custom annotation, when it is tapped, the row of this on table will be highlighted,
I tried adding gesture recognizer to custom annotation, but it does not work.
I alsa tried annotation did select method, the result is the same.
My code is as below;
UITapGestureRecognizer *singlePress =[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSinglePress:withIndex:temp:)];
[annotationView addGestureRecognizer:singlePress];
[annotationView setUserInteractionEnabled:YES];
Add callout button on annotationView such like,
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
myCustomAnnotationView.rightCalloutAccessoryView = rightButton;
and call method
-(void)showDetails:(UIButton *) calOutBtn
{
// do your stuff;
}
EDITED:
In your Case you need to add such like,
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(TouchViewMethod:)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[ annotationView addGestureRecognizer:gestureRec];
I have added an ui button to a disclouser button
And implemented selector method
but the action selector doesn't respond to the button why and what to do
//To add a discloser button to show route map
UIButton *detailBtn=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.leftCalloutAccessoryView=detailBtn;
[detailBtn addTarget:self action:#selector(showRoute:) forControlEvents:UIControlEventTouchUpInside];
DisplayMap *ann=(DisplayMap *)annotation;
detailBtn.tag = ann.detailButtonTag;
UIButton *btnOnPopup=[[UIButton alloc] initWithFrame:CGRectMake(- 207, -6, 300, 43)];
btnOnPopup.backgroundColor = [UIColor whiteColor];
[btnOnPopup addTarget:self action:#selector(showRoute2:) forControlEvents:UIControlEventTouchUpInside];
btnOnPopup.tag = ann.detailButtonTag;
btnOnPopup.userInteractionEnabled = YES;
[detailBtn insertSubview:btnOnPopup aboveSubview:detailBtn];
the selector
[btnOnPopup addTarget:self action:#selector(showRoute2:) forControlEvents:UIControlEventTouchUpInside];
(Second button btnOnPopup) is not working.
Your btnOnPopup is out of detailBtn's bounds so touches won't even get to it. Adding a button to a button is not really a great practice anyway. Adding it to a button's superview might be a little bit better (and will make your touches work, but you might need to adjust frame)
[detailBtn.superview insertSubview:btnOnPopup aboveSubview:detailBtn];
Add view and tap gesture recognizer instead of button:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureHandler:)];
[singleTap setNumberOfTapsRequired:1];
[view addGestureRecognizer:singleTap];
...
[detailBtn addSubview:view];
...
- (void)tapGestureHandler:(UIGestureRecognizer *)gestureRecognizer
{
// your code for tap handler is here
}
I am making an ipad app in which I have tableView with custom it works fine but I want that when user clicks on add button in custom cell then it should get the position x and y of that button and then set the imageView to the center of that button.
I have searced code it gets points but how to add make this for cell.addButton as cell is custom.
[cell.imageButton addTarget:self action:#selector(imageButtonActionOne:) forControlEvents:UIControlEventTouchUpInside];
[cell.addButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
Here is the code for touch
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
But in this way it does not call the method which is on cell.addButton imageButtonAction.
As I understand from you , you need to set an image in a UIButton upon tap.
If you added the UITapGestureRecognizer you removed the default tap recognition and now only this would work:
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
You should remove the above code and only set this:
[cell.addButton addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
where the imageButtonAction: is :
- (void )imageButtonAction:(UIButton *) b
{
[b setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
}
If you want to add an image next to the button let's say in the left of the button then the function should look like this:
- (void )imageButtonAction:(UIButton *) b
{
UITableViewCell *cell = (UITableViewCell*)[b superview];
NSInteger imgWidth = 50;
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(b.frame.origin.x - imgWidth,b.frame.origin.y,imgWidth,b.frame.size.height)];
img.backgroundColor = [UIColor redColor];
[cell addSubview:img];
[img release];
}
UIButton *btn = (UIButton *)sender;
UIImageView *backimage = [[UIImageView alloc] initWithFrame:CGRectMake(10,0,312,105)];
//If you want to do this set interaction enabled.
backimage.userInteractionEnabled = YES;
backimage.image=[UIImage imageNamed:#"popupj.png"];
tab1 = [UIButton buttonWithType:UIButtonTypeCustom];
[tab1 setFrame:CGRectMake(-1,2,293,58)];
[tab1 setTitle:#"Record Video" forState:UIControlStateNormal];
[tab1 addTarget:self action:#selector(tab1Action) forControlEvents:UIControlEventTouchUpInside];
[tab1 setImage:[UIImage imageNamed:#"popuptab1j.png"] forState:UIControlStateNormal];
[backimage addSubview:tab1];
now i am adding button to imageView but it does not get clicked
For UIButton you can use directly button function
-(IBAction) imageButtonAction:(id) sender
{
UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
UIButton *btn = (UIButton *)sender;
//[btn setimage:[UIImage imagenamed:dimagename];
UIImageview *backimage = [[UIImageView alloc] initWithFrame:btn.frame];
[cell addSubview:backimage];
}