How can I reference by code a button's tag value which I assigned earlier?
What I'm trying to do is create a list in a table view that has checkbox images in each row. I only want one to be selected at a time within each section, so once one is selected, I would loop through the other buttons to unselect the others.
Below is the code how I assign each button in cellForRowAtIndexPath:
UIButton *addCheckButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addCheckButton.frame = CGRectMake(self.view.frame.size.width - 40.0f, 5.0f, 32.0f, 32.0f);
[addCheckButton setBackgroundImage:[UIImage imageNamed:#"checkbox_empty.png"]
forState:UIControlStateNormal];
[addCheckButton setBackgroundImage:[UIImage imageNamed:#"checkbox_full.png"]
forState:UIControlStateSelected];
[cell addSubview:addCheckButton];
[addCheckButton addTarget:self action:#selector(changeCheckState:) forControlEvents:UIControlEventTouchUpInside];
addCheckButton.tag = ([indexPath section] + 1) * 100 + [indexPath row];
I then can strip out the section and row of the selected button in my table view in the changeCheckState IBAction.
But I would also like something that I can say "Deselect buttons with tag 101, 102 and 103" within changeCheckState.
id my101Button = [view viewWithTag:101];
This question is one of those that are googled in seconds.
Related
I have an issue in an UITableView as I need to place a button on each row of the tableview and if i click the button, it gets selected at a time and selecting is like toggle. How to achieve this, please help me out to solve this problem.
Below is the following code that am trying to get it out. I am creating button on cellForRowAtIndexPath as shown below:
UIButton *Btn = [[UIButton alloc]init];
Btn .frame = CGRectMake(3, 10, 33, 30);
[Btn setImage:[UIImage imageNamed:#"deselect.png"] forState:UIControlStateNormal];
[Btn addTarget:self action:#selector(method:) forControlEvents:UIControlEventTouchUpInside];
Btn.tag=indexPath.row;
[cell.contentView addSubview:Btn];
and the method: for button action:
-(void)method:(UIButton*)sender
{
NSIndexPath *indexpath1 = [NSIndexPath indexPathForRow:sender.tag inSection:0];
Btn = (UIButton *)sender;
//Btn.tag=sender.tag;
[Btn setImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateNormal];
}
You have an array to store whether the button of a cell is selected or not. In the cellForRowAtIndexPath method, you should set 2 images for 2 states UIControlStateNormal and UIControlStateSelected of the button:
[Btn setImage:[UIImage imageNamed:#"select.png"] forState:UIControlStateSelected];
[Btn setImage:[UIImage imageNamed:#"deselect.png"] forState:UIControlStateNormal];
Then in the method: function, you switch the state of the button via Btn.selected based on the value in the array.
UIImage * btnLinkNormal = [UIImage imageNamed:#"select.png"];
[cell.linkButton setImage:btnLinkNormal forState:UIControlStateNormal];
// Add image to button for pressed state
UIImage * btnLinkHighlighted = [UIImage imageNamed:#"deselect.png"];
[cell.linkButton setImage:btnLinkHighlighted forState:UIControlStateHighlighted];
I have tableView with detailDisclosure button it works fine but i want to give title to detail disclosure button so i think i need to add custom UIButton for detailDisclosure is it possible to do like i want to do this without using custom cell.
If i want to give image it works fine but how may i give title to this button like comment.
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"loginN.png" ]];
Try this..
UIButton *btn = [[[UIButton alloc]init]autoerelease];
btn .frame = CGRectMake(210.0, 0.0, 90, 25);
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"loginN.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClickHandler:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubView: btn];
and in btnClickHandler do this to get indexPath
- (void)btnClickHandler :(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//Get your data source object from indexPath.row.
}
Why don't u set the a button as accessory view just like u try setting an image..??
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 80.0, 80.0);//Set the frame u need for the view
//Customize the button
[button setTitle:#"title" forState:UIControlStateNormal];
//And set it as accessory view
cell.accessoryView = button;
Here is the result...
Currently I have 14 buttons being created programmatically using a for loop, code below:
int buttonCount = 14;
for (int i=0; i< buttonCount; i++) {
//Create titleString from array object
NSString *stringFromInt = [NSString stringWithFormat:#"%#",[arrayForRound objectAtIndex:i]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(buttonSelected:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:stringFromInt forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"helvetica" size:19];
button.tag = i;
[self.view addSubview:button];
}
This works great to create the buttons, I can then populate the Answer Box with the value of the selected button:
-(void)buttonSelected: (UIButton *)sender
{
[_buttonOne setTitle:sender.titleLabel.text forState:UIControlStateNormal];
}
However after the button has been populated I would like to remove it from the screen. If i call button.hidden it simply hides the last button that was programmatically created. I am aware of button.tag and have tried to use this, but it feels like I almost need to do something like:
//Hide button for tag (i know this is incorrect syntax)
button for buttonTag: 3 setHidden;
Is there something similar or a way of doing this?
Update
The button that I am trying to hide is the one that was created programatically. So I want _buttonOne to take on the title of the create button (lets call that letterButton), and then hide letterButton from the view,
UIButton *yourBtn = (UIButton *)[self.button viewWithTag:3];
[yourBtn setHidden:YES];
(code posted by Oh Seung Kwon)
This code work perfectly but it hides the wrong set of buttons. (Hides _buttonOne and not letterButton).
I wonder if it would not be better to create the 12 buttons in nib and name them manually...There will never be more or less that 12.
When your button is tapped, you can set the hidden property on the action method's sender argument, which is the button that actually got tapped. This will hide the button which was tapped.
- (void)buttonSelected:(UIButton *)sender {
[_buttonOne setTitle:sender.titleLabel.text forState:UIControlStateNormal];
[sender setHidden:YES];
}
If you meant to retrieve the button with the tag of 3, you can use this code instead:
[[self.view viewWithTag:3] setHidden:YES];
I don't recommend that you use the tag property - you should use Interface Builder and an IBOutletCollection instead.
Like this
UIButton *yourBtn = (UIButton *)[self.view viewWithTag:3];
[yourBtn setHidden:YES];
You could get the view by tag use this message.
[self.view viewWithTag:3];
We always specific tag by macro just like
#define kFirstButtonTag (100)
or use
#define kButtonBeginTag (100)
Then use the macro to get tag.
And in a special number - Case 0, 1 or 2 is always use, begin your tag in a special number could avoid some problems
I want to create a button (not dynamically) bellow the last row in the table. All the rows and sections are fixed. Do I have to create another section and row just to add a button? and if yes how can I ajust the button to the same width and curvature of the cell?
Here is what I did. I create a specific section for the button and one row for it.
}else if(indexPath.section ==2)
{
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cellButton setFrame:CGRectMake(0.0f, 5.0f, cell.frame.size.width, 44.0f)]; // not filling the round parts of the cell
[cellButton setBackgroundColor: [UIColor yellowColor]];
[cellButton setTitle:#"test" forState:UIControlStateNormal];
[cell.contentView addSubview:cellButton];
}
return cell;
You don't. Set your button as the table footer view of your table view.
tableView.tableFooterView = myButton;
In my cellForRowAtIndex I initialize a button (it gets shown in every cell in the tableview):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f,cell.frame.size.height -15, 160.0f, 15.0f);
UIImage *buttonImage = [UIImage imageNamed:#"Image1.png"];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button.titleLabel setFont:[UIFont fontWithName:#"Arial" size:13.0]];
button.contentEdgeInsets = UIEdgeInsetsMake(1, 60, 0, 0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cell addSubview:button];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
It has a normal image and if the user clicks on the button, the image changes. Also I get the contents of the button.title from a XML. Therefore I tell it to get the right title from the web. This works fine, but sometimes (not all the time) and also randomly it changes the button from the next cell, or the cell three rows down. I don't know why and couldn't find anything while running with breakpoints/debugger.
-(void)customActionPressed :(id)sender
{
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *pathToCell = [self.tableView indexPathForCell:owningCell];
UIImage *buttonImage = [UIImage imageNamed:#"image2.png"];
[self doXMLParsing];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:pathToCell.row] pressed1]];
[sender setTitle:TableText forState:UIControlStateNormal];
((UIButton *)sender).enabled = NO;
}
So maybe someone can see where I went wrong. If you need more code, just say so.
[button addTarget:self action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
Try having UIControlEventTouchUpInside instead of UIControlEventTouchDown.
also you create a custom cell and then you can create a property in the class subclassing UITableViewCell for your button and change the image of button through that property.
Also Don't change the image from action of button .You can access the button through cell.yourbuttonName in didSelectRowAtIndexPath delegate of tableview and then change the image.