Any way I can get hold of which button I have pressed? - ios

Is there any way I can get hold of which button I have pressed?
The buttons are created programmatically using a for loop.
I have a scroll-view of images (i used buttons for the images) where the images are taken by the user from the camera. So after the user takes a picture, the "new" picture will appear in the scrollview with the "old" pictures. The pictures are shrink into smaller sizes so what I want is that when I click the button (of any image) the image will pop up in another view in the actual size.
The button of images is created using a for loop. However, I do not know how to get hold of which button the user press. For now, when i press the button (regardless of which image/button), the last picture that is taken will always show up.
Thanks for you time.

You can use the tag property of button.
When you create the button, put a tag to each image buttons.
for(int i = 0 ; i < your_no_images ; i++){
UIButton *button = [UIButton <yourbuttontype>];
-----
button.tag = i;
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[yourView addSubView:button];
}
Now in the button click action..
-(void)buttonClicked:(UIButton*)sender{
//if you has an array of UIImage's
UIImage *clickedImage = (UIImage *)[yourImageArray objectAtIndex:sender.tag];
}

IBAction methods, like those triggered upon clicking a button, include a sender argument. sender is the object that triggered the action, for example the button that was clicked. Like this:
- (IBAction)buttonWasClicked:(id)sender
{
NSLog(#"The %# button was clicked", (UIButton *)sender.currentTitle);
}

Related

Fastest way to sequential change of UIButton hidden or hide

I want to have a function that by pressing the 1st button, then the 2nd button will be appear. When the 2nd button appear, after pressing it, then the 3rd button will appear and so on until the 10th button.
Making IBAction for each button can do the job. But it will be very time consuming. And I have multiple of these sequence needed to be done.
Is there any other way is faster and simpler to get this job done?
Thanks
For this purpose, you have to use the tag property of UIButton. while creating the UIButton set the tag value. For example for button1 it should be 1, for button2 it should be 2, and so on...
After that set the same IBAction for all UIButtons. And inside that IBAction get the button based on tag value and unhide next UIButton.
-(IBAction)buttonClicked:(id)sender
{
UIButton *button = (UIButton *)[self.view viewWithTag:(sender.tag + 1)];
button.hidden = NO;
}
You should be use loop for these process . . .
(if second button not appear only on first button click)

Can we add button on scroll view

I want to select multiple images from gallery and want to upload but before uploading I want to add a cancel button which help's user in deleting the image/image's after selection if user wants to. So how can I add a cancel button on image view ? I've tried the below code for adding button but when I select the multiple image's or single image then this button is not visible.
UIImage *imgview1=(UIImage*)[UIImage imageNamed:#"overlay.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setImage:imgview1 forState:UIControlStateNormal];
button.frame = CGRectMake(25, 25, 60, 60);
[_scrollView addSubview:button];
I want to make the User Interface of the page like this image!
Unfortunately I can't comment yet, but could you provide a bit more clarification?
My understanding from the question is that you would like to have a scroll view with many buttons in it (the buttons will be selectable images), and on those buttons you would like to add an (x) button like in the image that will respond to taps.
If that is your question then I think the simplest answer is to add another button as a subview of the image when it is selected. When that subview button is tapped you can respond in a method like below:
(void)xTapped:(UIButton *)xButton
{
UIButton *myImage = xButton.superview;
// then unselect the superview here
}

How do I change the UIImage of a UIButton that is a property of a custom UICollectionViewCell upon tap?

I have a custom UICollectionViewCell that holds a UIButton with an image set. The purpose of this bottom is to allow users who are shopping via the app to add items to a list of their favourite items.
I have 2 versions of the image. One is a default greyed out heart and the other is a black heart. When a user taps to add an item to their favourites this method is fired:
Method fired on tap of heart:
- (void)addToFavouritesButtonTapped
{
NSLog(#"add to favourites button tapped");
}
In my cellForItemAtIndexPath method for my UICollectionView I have this:
_addToFavouritesButton = [cell addFavouriteButton];
[_addToFavouritesButton addTarget:_thisController action:#selector(addToFavouritesButtonTapped) forControlEvents:UIControlEventTouchUpInside];
Upon tapping the heart to save an item to favourites I'd like the image to change to my highlighted version which is the black heart.
How can I achieve this?
Set the other image as the one for the UIControlStateSelected and then when the button is tapped set the button to selected= YES, this will then change the image.
_addToFavouritesButton = [cell addFavouriteButton];
[_addToFavouritesButton addTarget:_thisController action:#selector(addToFavouritesButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[_addToFavouritesButton setImage:blackHeart forState:UIControlStateSelected];
- (void)addToFavouritesButtonTapped:(UIButton *)sender
{
NSLog(#"add to favourites button tapped");
sender.selected = YES;
}
You can use the button state to show a different image. Check the documentation for UIButton, there is a method setImage:forState: that you can use to set an image for normal state, highlighted state, selected state.
You can even do it in Interface Builder.
On a side note, if you want to do something else with the button once it's pressed you should do the following:
Append : to the selector
[_addToFavouritesButton addTarget:_thisController action:#selector(addToFavouritesButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
Receive the UIButton in the method
- (void)addToFavouritesButtonTapped:(UIButton *)sender
Then you can manipulate the button in that method

UIButton background image change from another button

Is it possible that I can change the background image of some button when I click on another button?
I basically have 10 buttons, and I want the user to know which button is currently clicked. So what happens is each button has 2 images. One for selected and one for not selected.
I want to create a function which will reset the background images of all the buttons. And in the method for each button, I will add this reset function and then change the background image of that specific button.
Can someone suggest how is that possible?
I know how to change the background image of a button when that button is clicked.
This how my buttons look:
- (IBAction)posterButton:(id)sender {}
- (IBAction)colorInvertButton:(id)sender {}
etc..
Look up the documentation for UIButton: configure each button like this:
[button setControlImage:selectedImage forState:UIControlStateSelected];
[button setControlImage:unselectedImage forState:UIControlStateNormal];
^^this can also be done in interface builder btw.
There are also the states UIControlStateNormal | UIControlStateHighlighted and UIControlStateSelected | UIControlStateHighlighted, but this is optional.
A button also has a selected state, inherited from UIControl.
If you want to have only one selected button at a time, try this (_selectedButton should be declared as an instance variable):
- (UIButton *)selectedButton {
return _selectedButton;
}
- (void)setSelectedButton:(UIButton *)b {
if(b != _selectedButton) {
_selectedButton.selected = NO;
b.selected = YES;
_selectedButton = b;
}
}

Changing a UIButton's image when it's tapped

I have a UIButton that I am programmatically creating and adding to a UITableViewCell.
I have successfully set it up so that if you tap the button and keep holding it down, it will change to the image that I have set for the "highlighted state", but this is not good enough.
When the user taps the button, I need it to completely change to the new image. If the user taps the button again, I want it to change back to the original image.
I want the image to change every time they tap. Right now it changes if they tap and keep holding it down, but it switches back to the original image as soon as they are done holding it down.
Here is the code that I have so far for my buttons:
UIImage *addFriendButtonImage = [UIImage imageNamed:#"SliderThumb-Normal-G"];
UIImage *addFriendButtonImageHighlighted = [UIImage imageNamed:#"SliderThumb-Normal"];
UIButton *addFriendButton = [[UIButton alloc]init];
addFriendButton.frame = CGRectMake(237, -10, 64, 64);
[addFriendButton setImage:addFriendButtonImage forState:UIControlStateNormal];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateHighlighted];
I also tried setting the state for the new image to "UIControlStateSelected" but all that does it make the original image a little darker. It doesn't even change to the new image, and once again it only shows it's effect if you are holding the button down.
Set the image for both the UIControlStateHighlighted and UIControlStateSelected states:
[addFriendButton setImage:addFriendButtonImage forState:UIControlStateNormal];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateHighlighted];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateSelected];
Then listen for UIControlEventTouchUpInside:
[addFriendButton addTarget:self action:#selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
Then update the selected state:
- (void)handleTouchUpInside:(UIButton *)sender {
sender.selected = !sender.selected;
}
What you want is to set new image with [addFriendButton setImage:someImage forState:UIControlStateNormal]; every time the user taps on addFriendButton (look for event UIControlEventTouchUpInside).
If you want you can still assign appropriate highlight image on [addFriendButton setImage:someImage_highlighted forState:UIControlStateHighlighted];
You need to create an IBAction method for your button. If you are using a storyboard to design your view controllers go to the respective view controller in the assistant editor (tap on the icon in the upper right corner of the Xcode window), CTRL+drag the a line from the button to the .h file of your view controller's class like this:
Select Action and enter a named for the method like touchUpInsideButton.
Repeat the same steps to create a property for your button. (But select Outlet instead of Action and give your button a name like myButton.) Now you should have these two lines in your .h file:
#property (strong, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)touchUpInsideButton:(id)sender;
Now go to the .m file of your view controller. Search for the newly created action method - (IBAction)touchUpInsideButton:(id)sender and enter the following code to change the button's image when the user taps the button:
- (IBAction)touchUpInsideButton:(id)sender {
UIImage *myImage = [UIImage imageNamed:#"myImageName"];
[self.myButton setImage:myImage forState:UIControlStateNormal];
}
(You can put any code inside this method that is executed whenever the user taps the button.)
I would recommend this method if you are using a storyboard. If not please refer to #Corey's reply.

Resources