Add checkmark image to multiple array of buttons - ios

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

Related

Enabling and disabling multiple buttons in view issue

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!

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];

I have 15 UIButton in my iPhone project, after clicking on button one image is pop up and all images are different for different button?

I have 15 UIButton in my iPhone project, after clicking on button one image is pop up and all images are different for different buttons, how could I achieve this by using touch?
How should I call proper image for particular button?
I dont want to take 15 UIButtons and 15 UIImageView .
You could have all 15 buttons hooked to one single "IBAction" method.
When the method is called, the parameter that is sent (e.g. "(id) sender") is the object that sent the message.
If you assign different tags to each button, you'll know which button called the action method and then you could have a different image appear for each different button.
All you need here is one "IBAction" method and one "IBOutlet" for a single image view. You will still need to have some kind of logic to load 15 different images into that single image view, though.
Here is how you can do:
This is called after any button is clicked:
-(IBAction)buttonClicked:(UIButton *)sender {
if(sender.tag == 0){
NSLog(#"Button one");
} else if(sender.tag == 1){
NSLog(#"Button two"); ... // like wise do it for 15 buttons
}
}
All you have to do is assigne tag to the button when you create it.
I think you must be running a for loop 15 times to create those buttons,
so:
for(i=0;i<15;i++){
//create button..
button.tag = i;
}
Have a background image of the view with all the 15 button information, which is divided to 15 locations to detect the touches.
Get the touch location (co-ordinates) and based on the particular location that is pre defined, update the image in the popup UIImageView accordingly.
this might help to get the location of the touch in the view.
UIView touch location coordinates
You could use IBOutletCollections, this requires that you hook up the 15 UIButtons in a Xib.
The following code assumes you have a collection of N-buttons. For a given gesture, it will iterate over the buttons in the collection and determine if the location of the touch is within a buttons frame.
Edit: IBOutletCollections are available in iOS 4 and up.
Assuming you have the following as properties:
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *collection_buttons;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
And the following synthesizes:
#synthesize collection_buttons = _collection_buttons, imageView = _imageView;
Then you could use the following code on gesture call back.
-(IBAction) tapGestureOnButton:(UITapGestureRecognizer *) gesture {
CGPoint point = [gesture locationInView:self.view_buttonContainer];
for( int index = 0; index < self.collection_buttons.count; index++ ) {
if( CGRectContainsPoint(((UIButton *)[_collection_buttons objectAtIndex:index]).frame, point)) {
//Button at index has been pressed
[imageView setImage:[button backgroundImageForState:UIControlStateNormal]];
break;
}
}
}

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.
}

How to use multiple buttons in the same window?

Using XCode 3.2, What I'm trying to do is have the user input data and then they have three choices of what to do with that data, i.e. there are three different options or three buttons that I want to have in the same window. I can't figure out to have that coded...
right now i have one button that is activated using the
-(IBAction)buttonPressed
{ (formula)
}
how do I do this with multiple buttons?
I've looked for answers but they are a bit different than what Im looking for. Thank you so much!
while creating buttons set mybutton.tag=0;like this.change 0 to ur own and compare them in buttonpressed function
use sender to
-(IBAction)buttonPressed:(id)sender
{
UIButton *temp=(UIButton*)sender;
if([temp tag] == 0)
(formula)
//button 0
}
if([temp tag] == 1)
(formula)
button 1
}
}
Button press code should look like this:
- (IBAction)buttonPressed:(id)sender {
UIButton *senderButton = (UIButton *)sender;
if (senderButton == btnOK) {
// this is the OK button
} else if (senderButton == btnCancel) {
// this is the Cancel button
}
}
A better way, though, is probably to write three separate methods, and hook each one up to its corresponding button in IB.

Resources