Select two buttons which will lead to an "if statement" (Objective-C) - ios

I'm trying to get back into iOS programming, but it's been a while so I've kind of lost it.
I'm trying to make a simple single view game. I have four buttons. Button 1, button 2, button 3, and button 4.
What I need to happen is: When I press a button, the button gets selected, and then I want the user to be able to press any other button, which will then lead to an if statement that I'm gonna code later.
Also, I want the user to be able to unselect the previously selected button by just clicking it again. So that he or she can select another button of their choice, to start with.
I'd really appreciate this simple setup of code, since I have no clue how I'm gonna do it.

The idea is to change the state of the other buttons in the selector (IBAction) for the 1st button after it is pressed and control goes back to the user.
Whatever happens to the 3 button seems to imply that you just want them set or unset then evaluated at the same time - the more appropriate class to use is a UISwitch so you can set - unset them all at once without triggering an action. Of course you may use buttons if you wish but typically each button will trigger a selector instead of your code patiently waiting for the user to make up his mind in setting the options before your code starts again.
In case you forgot:
- (void)viewDidLoad {
[super viewDidLoad];
[Button1 addTarget:self action:#selector(Button1_pressed:)
forControlEvents:UIControlEventTouchUpInside];
[Button2 addTarget:self action:#selector(Button2_pressed:)
forControlEvents:UIControlEventTouchUpInside];
Button2.enabled = NO;
[Button3 addTarget:self action:#selector(Button3_pressed:)
forControlEvents:UIControlEventTouchUpInside];
Button3.enabled = NO;
.... }
-(IBAction) Button1_pressed:(ID) sender {
Button2.enabled = YES;
Button3.enabled = YES;
Button4.enabled = YES;
}

the above functionality can be easily achieved by managing flag to know if button is already selected or not.
set slected=1; when user press the button,
selecting again will change flag to selected=0 will help you to get selected status of button easily.

Take a BOOL variable isSelected.
isSelected = ! isSelected;
if(isSelected){
button.enabled = NO;
}
else{
button.enabled = YES;
}

Related

How can I create one UIButton that allows me to perform 2 IBAction iOS - Objective-C

How can I create one UIButton that allows me to perform 2 IBAction. For example after a user presses "Follow" the button turns to "Unfollow" and vice a versa. Is this best to accomplish with 2 buttons or 1 button and using different states. I've research how to acomplish this with one button but haven't found any solution. Thanks!
You can achieve that using a single button. You need to set the title of your buttons to each of it's states and toggle the states in the IBAction method when a selection or de-selection occurs:
[yourButton setTitle:#"Follow" forState:UIControlStateNormal];
[yourButton setTitle:#"Unfollow" forState:UIControlStateSelected];
And implement the IBAction method like:
- (IBAction)btnClicked:(UIButton *)sender
{
sender.selected = !sender.selected;
if (sender.selected)
{
// Currently selected the button and it's title is changed to unfollow
// Do your selection action here
}
else
{
// Currently de-selected the button and it's title is changed to follow
// Do your selection action here
}
}

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

Printing a page in IOS

I would like to print a page in my app made up of textfields and 3 labels onto paper. The issue is I have would like to hide the "print" button when it's pressed so that it doesn't show up on paper.
Can someone show me some example code for this?
[button addTarget:self action:#selector(printPage:) forControlEvents:UIControlEventTouchUpInside];
- (void)printPage:(id)sender
{
// Your print command code
// If you want to set the button visible again, you can implement completion handler callback using block implementation
((UIButton *)sender).hidden = YES;
}

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 set the UIButton state to be highlighted after pressing it

I have a typical requirement wherein I need to keep a button in highlighted state after pressing it. I need to perform a task which should work only when a button is in highlighted state. Actually I am setting a button state to highlighted programatically.
[sender setHighlighted:YES];
And once the button is in highlighted state i need to perform another action.
- (IBAction)changeState: (UIButton*)sender
{
if (sender.highlighted == YES)
{
[self performSomeAtion:sender];
}
}
But, to my horror, whenever I press any button, the above condition is becoming true and the action is being performed repeatedly. Is there any way in which i can keep a UIButton's state to be highlighted after pressing it?
EDIT - Actually I need to perform 3 different actions for 3 different states of the button. I am already making use of selected state and normal state. Now, I need to make use of the highlighted state.
[sender setSelected:YES];
or you can simulate this effect with two image for your UIButton (notselectedimage.png and selectedimage.png), then keep track button state with a BOOL variable like BOOL buttonCurrentStatus;. Then in .h file:
BOOL buttonCurrentStatus;
and in .m file
// connect this method with Touchupinside function
- (IBAction)changeState:(UIButton*)sender
{
/* if we have multiple buttons, then we can
differentiate them by tag value of button.*/
// But note that you have to set the tag value before use this method.
if([sender tag] == yourButtontag){
if (buttonCurrentStatus == NO)
{
buttonCurrentStatus = YES;
[butt setImage: [UIImage imageNamed:#"selectedImage.png"] forState:UIControlStateNormal];
//[self performSomeAction:sender];
}
else
{
buttonCurrentStatus = NO;
[butt setImage:[UIImage imageNamed:#"notSelectedImage.png"] forState:UIControlStateNormal];
//[self performSomeAction:sender];
}
}
}
- (void)mybutton:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = ![button isSelected]; // Important line
if (button.selected)
{
NSLog(#"Selected");
NSLog(#"%i",button.tag);
}
else
{
NSLog(#"Un Selected");
NSLog(#"%i",button.tag);
}
}
The highlighted state is used to highlight the button while it is being touched. A touch down event in the button highlights it. You should use the "selected" state instead.
If what you want to do is perform an action after the button is pressed, don't attach your method to the state change event, attach your method to the TouchUpInside event.
I just find a way, so I share it, just in case...
I kept my UIButton and set one image for each state (so you could go up to a 4 states button).
I set the UserInteractionEnabled to NO -> This button won't receive any touch.
The purpose of this first button is to show a state
I create a second custom UIButton with the same frame than the first one. For this one, none image will be set for the state (it's a fully transparent button). The purpose of this button is to catch the touch event. So I added a target to this button on the TouchUpInside event. And then when the event is fired, I change the state of the first button to Disabled, Highlighted, Selected, or none of these state (= Default state).
Everything is working like a charm!
The way you describe it, you'd be better off subclassing UIView to create your own three-state button.
Actually, you should even implement your own multistate buttonView, and manage the state it's in internally via an array of PNG for the looks and an array of states to know how many times it's been pressed.
Use [sender setSelected: YES];, I think it will be useful to you.
UIButton *btn_tmp=sender;
if(!(btn_tmp.selected))
{
[btn_temp setHighlighted:YES];
}
For iOS 7 only: you should consider setting the image renderMode to UIImageRenderingModeAlwaysTemplate. You can then use the tintColor to represent various states.
see How to apply a tintColor to a UIImage?
and
see Tint a UIView with all its subviews
The solution is tricky but it's possible.
The problem is that you tried to change the highlighted status in the button action method, which I suppose makes a clean up or check process at the end of the action and switch the highlighted status. When you try to debug it you get the highlighted = 1 but it will change at the end.
Strange but your "3 statuses button" is sometimes useful, when you'd like to keep a button in "highlighted" mode like the "selected" mode to get different action depending on the 3 statuses.
The only problem that you couldn't analyze this or switch it to highlighted mode in the button action method as this will switch to highlighted mode immediately as the user push it AND switch it back at the end.
The solution is using a dispatch.
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[theButton setHighlighted:YES];
});
This will do the trick and you could use the 3 statuses.
According to apple, UIButton has a property of imageView:
Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance and behavior of the button’s view
This means that you can set in the IB (in the storyboard) a picture for this button and set the highlighted picture:
Open the Attribute inspector.
Under Button section, choose an image.
In the same section, change the State Config to Highlighted. Notice the image you chose under default is now gone and now you can set a new picture for the Highlighted.
Now you have a button with 2 state config and all you have to do during runtime to change the button.highlighted = true. Also, check the UIControl under Configuring the Control’s Attributes for more states.
You can also do it programatically as follows:
Swift (and almost the same in Objective-C):
// Setting the highlighted image
self.someButton.imageView?.highlightedImage = UIImage(named: "imageNameFromImageAssest")
// someButton will now some the highlighted image and NOT the image set in the IB
self.someButton.imageView?.highlighted = true

Resources