Enabling and disabling multiple buttons in view issue - ios

I have a view with 12 buttons. I want to be able to perform the action's button just once when i tap it and then set that specific button disabled. When i tap on another button i want the same thing to happen + set the other buttons enabled. Is there a solution?

You can give to each button a tag, and when you tap on a button using method viewWithTag you can enable and disable each button:
When you create UIButton, you can add:
UIButton *button0 = ...
button0.tag = 0;
UIButton *button1 = ...
button1.tag = 1;
//and so on
In every action of the buttons, you must pass the id object like this:
-(void)tapButtonOne:(id)sender {
//with this sender you can retrive the tag of the button clicked
UIButton *button = sender;
int buttonTag = button.tag
//now you can check every button and enable the other that haven't the same buttonTag
//with the first tag = 0 plus 12 the last tag will be 11 so i<12
for (int i = 0; i<12; i++) {
//self.view if you have added the buttons on self.view, otherwise you must write your view
UIButton *buttonTemp = (UIButton *)[self.view viewWithTag:i];
if(buttonTemp.tag != buttonTag) {
buttonTemp.enabled = YES;
}
else {
buttonTemp.enabled = NO;
}
}
}

Instead of creating an outlet, create an IBOutletCollection.
When you drag from the interface builder to your app, you can change the "Outlet" selection to "Outlet Collection".
Once you've done that for the first button, control drag all your buttons and link them to the same collection.
Connect all buttons to the same IBAction and make sure it specifies the sender (this is the default).
Then, in your action:
for (UIButton* button in self.yourOutletCollection){
if (button == sender){
// this is the button that was tapped
} else {
// all the other buttons go here
}
}
Enjoy!

Related

Add checkmark image to multiple array of buttons

I am trying to add a checkmark button when user select a button, so I create two IBOutletColletion both for buttons and check mark image :
The next step is when user select each of of buttons a checkmark image appears on above of each image,So in this part I have a problem which is checkmarks all show and all hide ! not when user select the specific button . Here is my code :
- (void)viewDidLoad {
//Hide checkmarks when app opens
for (UIImageView*checkMark in _checkMarkArray) {
checkMark.alpha = 0;
}
}
- (IBAction)button1:(id)sender {
for (UIButton*button in _ButtonsArray) {
if (sender == button) {
for (UIImageView*checkMark in _checkMarkArray) {
checkMark.alpha = 1;
}
}
}
}
Use Tag (Where Button and Above image tag are same)
- (IBAction)button1:(id)sender {
for (UIImageView*checkMark in _checkMarkArray) {
if(sender.tag == Checkmark.tag && sender.isSelected)
{
checkMark.alpha = 1;
}
if(sender.tag == Checkmark.tag && !sender.isSelected)
{
checkMark.alpha = 0;
}
}
}
}
better solution
create Two image
one with only Heart and another image is heart With Checkmark
set Only heart image in button's Normal state (deSelected)
and set combined image in selected state
Another solution I would suggest is to create a custom UIButton class, which contains two UIImageViews. In the button action method make the checkmark image hidden or visible accordingly. In this case you just need two images, one heart image and one tick image.
Your CustomButton.h will look something like this :-
#property (nonatomic, strong) UIImageView *checkMarkImageView, *backgroundImageView;
Give proper frames for the ImageViews in CustomButton.m file. Now in the view or view controller where you are planning to give the button action, change the checkmark image view's state accordingly. So the button action will look something like this :-
-(void)buttonClicked:(UIButton *)sender {
if (sender.checkMarkImageView.hidden == YES)
sender.checkMarkImageView.hidden = NO;
else
sender.checkMarkImageView.hidden = YES;
}

UIButton Changes Variable When Tapped?

I have multiple UIButtons - redButton, greenButton, yellowButton, etc. When you tap one of these buttons, another UIImageView will change its image accordingly to, say, redImage, greenImage, yellowImage.
How would I implement this? I've tried this:
[self.redButton addTarget:self action:#selector(changeColour:) forControlEvents:UIControlEventTouchUpInside];
[self.greenButton addTarget:self action:#selector(changeColour:) forControlEvents:UIControlEventTouchUpInside];
[self.yellowButton addTarget:self action:#selector(changeColour:) forControlEvents:UIControlEventTouchUpInside];
And in changeColour:
-(void)changeColour:(id)sender{
// How do I know which button was pressed?
}
All I'd like to know is which button was pressed. Preferrably, I would like it so when a button is tapped, it changes an int variable called colour. So, something like:
if greenButton was tapped, change int = 1. If redButton was tapped, change int = 2.
I've searched StackOverflow and Google and the results say to include a method in the #selector of action:. So what would I have to put in changeColour to recognise which button called changeColour?
-(void)changeColour:(id)sender
{
if (sender == self.redButton) {
// redButton action
} else if (sender == self.greenButton) {
// greenButton action
} else if (sender == self.yellowButton) {
// yellowButton action
}
}
to do these kinds of identification the sender attribute was introduced.
You can use tag property of UIView.
i.e.
self.redButton.tag = 1;
self.yellowButton.tag = 2;
In the chnageColor method, you can retrieve the same.
-(void)changeColour:(id)sender
{
// How do I know which button was pressed?
UIButton *btn = (UIButton *)sender;
if(btn.tag == 1)
//Red is pressed..
//and you can check for other tags...
}
Tap on the UIButton in the Interface Builder, and change the tag property of it in Attributes Inspector. Then access this tag property programmatically in your view controller to get to know which button was pressed.

make button toggle method — objective-c

I have 4 buttons. When they are taped they each create a UIView underneath it, they expand under the buttons. I want the view to go back to its original when the buttons are taped a second time. How is this done?
Set an instance BOOL and flip it accordingly. In the IBAction the button is tied to check the BOOL and call the appropriate method to adjust the view.
You can use the selected property of your button, something like:
-(void)yourButtonIsTapped:(UIButton*)button {
if(button.selected) { //first time
//expand the view
button.selected = NO;
}
else { // second time
//hide view
button.selected = YES;
}
}
You can link the buttons from the IB th this method for the touchUpInside event but you will have to change the return type from void to IBAction.
And I think there are several other solutions for this case but this is the faster one and the easiest to explain.
Set every tag button to 0, change it to the opposite value when you press the button. Here is what you'll get:
First time that you press the button: button's tag = 0, expand the view and turn it to 1.
Second time you press the button: button's tag = 1, collapse it and turn it to 0.
So you haven't to create a bool for every button but you can use your button's tag variable.
Code should be like this.
- (void)handleButton:(UIButton *)button{
if(button.tag == 0) { <expandButton> }
else { <collapseButton> }
button.tag = !button.tag;
}
You can remove a view simply by calling
[viewUnderButton removeFromSuperview];

Dynamically updating buttons on a view in IOS?

I hope I can ask this question clearly, I have a viewController that I build programmatically (not with NIB), this view controller has two buttons I draw on the lower portion of the view
"prev" and "next" what I'd like to do is, when I've reached the "end" I'd like to only draw the "prev" button, and not the "next", and vice-versa, when I'm at the beginning, I'd like to only draw the "next" and not the prev.
Any general ideas on how to approach this ?
Thanks,
uba
You can use "hidden" property of UIButton:
if (page == firstPage) {
self.myButtonPrev.hidden = YES;
} else {
self.myButtonPrev.hidden = NO;
}
if (page == lastPage) {
self.myButtonNext.hidden = YES;
} else {
self.myButtonNext.hidden = NO;
}
The first thing to do is to addTarget for your button to listen to touch events
[yourButtonNameHere addTarget:self action:#selector(yourCallBackFunctionNameHere:) forControlEvents:UIControlEventTouchUpInside];
What this does is whenever your button is pressed it calls the yourCallBackFunctionNameHere function. Do this for the other button too.
The semi colon after the function indicates that the function has to send the information of the UIElement that caused the event to occur.
Assign tags to both the buttons.
youButtonNameHere.tag=yourTag;
In the function check which button is sending the UIcontrolEvent by comparing the tags
- (void)yourFunctionNameHere:(id)sender {
UIButton *yourButton =(UIButton *)sender;
if(yourButton.tag==501){
// logic to check if this is the first or last page and act accordingly
yourButton.hidden = YES / NO based on what you want to do.
}else{
// logic to do otherwise.
}

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

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);
}

Resources