Reputation: 16841
In my cell i have 3 buttons, when the user clicks on each button the following 3 method gets fired.
- (void) but1:(id) sender{
NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);
}
- (void) but2:(id) sender{
NSLog(@"Touched 2 %ld",(long)[(UIButton *)sender tag]);
}
- (void) but3:(id) sender{
NSLog(@"Touched 3 %ld",(long)[(UIButton *)sender tag]);
}
Imagine a user clicks on button 1, and then the but1
method should get fired, and also the background image of button 1 should change. How can make this change in the above method.
NB: I am using XIB
files.
I was able to access the NIB file that contains the cell .
UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.contextMenuTableView registerNib:cellNib forCellReuseIdentifier:@"cell"];
Now how can i access the button he touched and change the image ?
UPDATE
This is a table view. And in each cell there are 3 buttons, I should be able select and deselect one of these buttons in the table.
For example if only 1 button in the cell can be selected. When the cell is selected i will show an image. And when it's not i will show the default image.
Upvotes: 0
Views: 665
Reputation: 1491
To Know which button is pressed you can set Tag for every button. From that tag you will get which button is taped
In interface Section ..
int count;
In viewDidLoad
count=0;
- (IBAction)ButtonClicked:(id)sender {
NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);
if(count%2==0)
{
[((UIButton *)sender) setBackgroundImage:[UIImage imageNamed:@"imageA.png"] forState:UIControlStateNormal];
}
else
{
[((UIButton *)sender) setBackgroundImage:[UIImage imageNamed:@"imageB.png"] forState:UIControlStateNormal];
}
[(UIButton *)sender setTitle:@"" forState:UIControlStateNormal];
count=count+1;
}
Upvotes: 0
Reputation: 2958
you can also use this
button1.tag = 0;
button2.tag = 1;
button3.tag = 2;
[button1 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"selected_image.png"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:@"selected_image.png"] forState:UIControlStateSelected];
[button3 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button3 setImage:[UIImage imageNamed:@"selected_image.png"] forState:UIControlStateSelected];
Add a common selector( buttonClicked:
) to all the buttons and implement the selector as follows
- (void)buttonClicked:(id)sender
{
[button1 setSelected:NO];
[button2 setSelected:NO];
[button3 setSelected:NO];
if ([sender isKindOfClass:[UIButton class]])
[sender setSelected:YES];
}
Upvotes: 0
Reputation: 3389
May be this way you can archive it,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *btn1 = (UIButton *) [cell viewWithTag:1] ;
UIButton *btn2 = (UIButton *) [cell viewWithTag:2];
UIButton *btn3 = (UIButton *) [cell viewWithTag:3];
[btn1 setTag:((indexPath.row*10)+1)];
[btn1 setTag:((indexPath.row*10)+2)];
[btn3 setTag:((indexPath.row*10)+3)];
[btn1 addTarget:self action:@selector(but1:) forControlEvents:UIControlEventTouchDown];
[btn2 addTarget:self action:@selector(but2:) forControlEvents:UIControlEventTouchDown];
[btn3 addTarget:self action:@selector(but3:) forControlEvents:UIControlEventTouchDown];
return cell;
}
- (void) but1:(UIButton *) sender{
NSLog(@"Touched 1 %ld",((long)[(UIButton *)sender tag])%10);
[sender setBackgroundImage:[UIImage imageNamed:@"newImage.png"] forState:UIControlStateNormal];
NSInteger row = ((long)[sender tag])/10;
NSIndexPath *indexPathOfYourCell = [NSIndexPath indexPathForRow:row inSection:0];
[self.tableview beginUpdates];
[self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableview endUpdates];
}
- (void) but2:(UIButton *) sender{
NSLog(@"Touched 2 %ld",((long)[(UIButton *)sender tag])%10);
[sender setBackgroundImage:[UIImage imageNamed:@"newImage.png"] forState:UIControlStateNormal];
NSInteger row = ((long)[sender tag])/10;
NSIndexPath *indexPathOfYourCell = [NSIndexPath indexPathForRow:row inSection:0];
[self.tableview beginUpdates];
[self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableview endUpdates];
}
- (void) but3:(UIButton *) sender{
NSLog(@"Touched 3 %ld",((long)[(UIButton *)sender tag])%10);
[sender setBackgroundImage:[UIImage imageNamed:@"newImage.png"] forState:UIControlStateNormal];
NSInteger row = ((long)[sender tag])/10;
NSIndexPath *indexPathOfYourCell = [NSIndexPath indexPathForRow:row inSection:0];
[self.tableview beginUpdates];
[self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableview endUpdates];
}
Enjoy Coding !!
Upvotes: 0
Reputation: 1415
- (void) but1:(id) sender{
[but1 setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
forState:UIControlStateNormal];
NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);
}
try this code, this might help you
Upvotes: 0
Reputation: 1120
You can change the image of the sender as
- (void) but1:(id) sender{
NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);
[((UIButton *)sender) setImage:[UIImage imageNamed:"image.png"] forState:UIControlStateNormal]
}
Good luck
Upvotes: 2
Reputation: 3132
Try something like
UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];
Upvotes: 1