Changing a UIButton to be turned off - ios

I have a simple button with two images for each UIControlStates.
I want this button to behave as a control and as indicator so that I can press it down - "activate" and press again "deactivate", While some other functions can cause it to be turned off ("deactivate").
My question is how can I change it's state from selected to not-selected?
Changing the selected property would not do the trick :)

You can disable the button so it can't be press able.
yourButton.enabled = NO;
and When you want it back to be press able, then you can enabled it
yourButton.enabled = YES;

Simplest way would be consider button as switch and change it's state according switch on/off. For example use BOOL variable which upon button touch gets its value YES or NO while according to it button gets its image.
- (void) buttonTouched:(id)sender
{
switchOn = !switchOn;
UIImage *buttonImage = nil;
if (switchOn == YES)
{
buttonImage = [UIImage imageNamed:#"on.png"];
}
else
{
buttonImage = [UIImage imageNamed:#"off.png"];
}
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImage forState:UIControlStateSelected];
}
if you need to programmatically set button "disabled":
- (void) setButtonDisabled
{
switchOn = YES; //calling buttonTouched: will turn it to NO
[self buttonTouched:nil];
}

Related

Dynamic Radio Button in Objective C

I want to design a dynamic radio button in my app
for (int f = 0; f<arr.count; f++) {
UILabel *lbl = [[UILabel alloc]init];
lbl.frame = CGRectMake(radio_x+10,radio_y+5 , radio_w, radio_h);
lbl.text = arr[f];
lbl.textColor = [UIColor blackColor];
[self.sub_View addSubview:lbl];
self.yourButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosOfTxt,radio_y , 20, 20)];
[self.yourButton setImage: [UIImage imageNamed:#"RadioButton-Selected.png"]forState:UIControlStateNormal];
[self.yourButton setImage: [UIImage imageNamed:#"RadioButton-Unselected.png"]forState: UIControlStateSelected];
[self.yourButton setTag:baseRadioTag+f];
[self.sub_View addSubview:self.yourButton];
[self.yourButton addTarget:self action:#selector(radioSelected:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)radioSelected:(UIButton*)sender {
self.yourButton.selected = false;
sender.selected = !sender.selected;
self.yourButton = sender; }
I did like this , this is working like , selecting all the options , i want to select only one option at once , if I selected option 1 -> option 2 should be unselected . If I selected option 2 -> option 1 should be deselected.Please help me to do this .
First of all to make things easier
create a array which can hold reference of all the buttons
#interface ViewController ()
{
NSMutableArray *btnArray;
}
Create an object of Array
- (void)viewDidLoad {
[super viewDidLoad];
btnArray = [NSMutableArray new];
/// Here is your code for creating buttons
}
Now add all created buttons to this array
// this is last line from your code where you create your UI
[self.yourButton addTarget:self action:#selector(radioSelected:) forControlEvents:UIControlEventTouchUpInside];
// add this line to add your button to array
[btnArray addObject:self.yourButton];
}
Now when you click button radioSelected method is called
NOTE -: Here can be two cases according to me, but i don't know which one you require so i am explaining both of them
CASE FIRST :-When you select or deselect buttons final output can be that there can be no button selected(all can be deselected)
-(void)radioSelected:(UIButton*)sender{
// here if button is selected, deselect it otherwise select it
[sender setSelected:!sender.isSelected];
for (UIButton* btn in btnArray) {
// here we mark all other button as deselected except the current button
if(btn.tag != sender.tag){
// here when you deselect all button you can add a if statement to check that we have to deselect button only if it is selected
if(btn.isSelected){
[btn setSelected:NO];
}
}
}
}
CASE SECOND:-When atleast one button will be selected
-(void)radioSelected:(UIButton*)sender{
// here if current button is already selected we will not allow to deselect it and return
if(sender.isSelected){
return;
}
// here working is as same as described above
[sender setSelected:!sender.isSelected];
for (UIButton* btn in btnArray) {
if(btn.tag != sender.tag){
[btn setSelected:NO];
}
}
}
You can test both cases and use according to your requirement.
Hope this works for you.
And let me know if you face any issues :)

restore UIImageView Image in IBAction

I am adding an image in IBAction, I want to restore the old image back once the button is tapped.
It is not the image in the UIButton, it's the image in the UIImageView
- (IBAction)findMe:(id)sender
{
[self.imageView setImage:[UIImage imageNamed:#"image2"]];
}
I want to restore#"image1". immediate once button is tapped
some thing i need is some thing similar to
button setImage:[UIImage imageNamed:#"clicked.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
but i don't want to change Button Image, but change UIImageView Image when clicked
You can use the following code
[yourButton addTarget:self action:#selector(touch2:) forControlEvents:UIControlEventTouchDown];
[yourButton addTarget:self action:#selector(touch3:) forControlEvents:UIControlEventTouchUpInside];
Now the selectors
-(void)touch2:(UIButton *)send
{
imageView.image= [UIImage imageNamed:#"image1"];
}
-(void)touch3:(UIButton *)send
{
imageView.image= [UIImage imageNamed:#"image2"];
}
Try it out:
- (IBAction)findMe:(id)sender
{
if (self.imageView.image) {
// store your old image here
NSLog(#"%#", self.imageView.image)
}
[self.imageView setImage:[UIImage imageNamed:#"image2"]];
}
Try this first create gesture for UIButton
UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleBtnLongPressgesture:)];
[buttonFindMe addGestureRecognizer:btn_LongPress_gesture];
When you press and release the button it calls this method.
-(void)handleBtnLongPressgesture:(UILongPressGestureRecognizer *)recognizer{
//as you hold the button this would fire
if (recognizer.state == UIGestureRecognizerStateBegan) {
//Change your image 1
}
//as you release the button this would fire
if (recognizer.state == UIGestureRecognizerStateEnded) {
//Change your image 2
}
}

Navigating between UIViewControllers, retaining selected button image

How would you save a selected button state after you press it? So when you switch between view controllers and come to the original view controller, the button stays pressed, until you press it again...
- (void)viewDidLoad
{
[super viewDidLoad];
addCheck = NO;
favCheck = NO;
}
- (IBAction)listButton:(id)sender {
UIImage *removeListImage = [UIImage imageNamed:#"removeList.png"];
UIImage *addListImage = [UIImage imageNamed:#"addList.png"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *favoriteviews = [defaults mutableArrayValueForKey:#"favorite_views_key"];
[defaults setObject:favoriteviews forKey:#"favorite_views_key"];
[defaults synchronize];
if (!addCheck) {
[addList setImage:removeListImage forState:UIControlStateNormal];
addCheck = YES;
[favoriteviews addObject:#"Apple"];
[favoriteviews addObject:#"Banana"];
[favoriteviews addObject:#"Celery"];
}
else if (addCheck) {
[addList setImage:addListImage forState:UIControlStateNormal];
addCheck = NO;
[favoriteviews removeObject:#"Apple"];
[favoriteviews removeObject:#"Banana"];
[favoriteviews removeObject:#"Celery"];
}
}
For the simplest cases, you can set the images for two different states of the control(button in this case).
And check for the states on each tap to change it. For example
if(![myButton isSelected]){
[myButton setSelected:YES];
// Do your proceedings when the button is selected here
}
else{
[myButton setSelected:NO];
// Do your proceedings when the button is unselected here
}
And when you set up your button in viewDidLoad, just set the button's images for those two states
[addList setImage:addListImage forState:UIControlStateNormal];
[addList setImage:removeListImage forState:UIControlStateSelected];
Alternatively, if you are setting up button in Interface builder, you can set those images for the two states there too.
It would be better add some logic to it to persist some kind of data to set the button's state depending upon that.

What is this button artefact when UIButton is selected?

I'm trying to make a UI Button a toggle button by using this code:
- (void)tapButton:(id)sender{
UIButton *button = sender;
if (!button.selected){
[self performSelector:#selector(highlight:) withObject:button afterDelay:0.0];
}else{
[self performSelector:#selector(removeHighlight:) withObject:button afterDelay:0.0];
}
}
- (void)highlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = YES;
}
- (void)removeHighlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = NO;
}
However, I'm getting a weird artefact when the button is in selected mode.
Not selected:
Selected:
Just make the button type to be "custom" instead of "system". I guess this weird bug is related to the new Tint feature on iOS7. It's probably buggy because your title property is an empty string #"".

Determine if view is hidden or not and change button state programmatically

I have three views and three buttons each button toggles the view (hidden = yes/no)
- (IBAction)switchOne:(id)sender {
[_firstPage setHidden:NO];
[_secondPage setHidden:YES];
[_thirdPage setHidden:YES];
}
- (IBAction)switchTwo:(id)sender {
[_firstPage setHidden:YES];
[_secondPage setHidden:NO];
[_thirdPage setHidden:YES];
}
- (IBAction)switchThree:(id)sender {
[_firstPage setHidden:YES];
[_secondPage setHidden:YES];
[_thirdPage setHidden:NO];
}
I want to set the background of the button depending if the view is hidden or not.
I have tried this but without results:
if (_firstPage.hidden == NO)
{
UIImage *buttonImage = [UIImage imageNamed:#"currentPage.png"];
[_pageOneButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:_pageOneButton];
} else if (_firstPage.hidden == YES) {
[_pageOneButton setBackgroundImage:nil forState:UIControlStateNormal];
}
The _pageOneButton keeps the background even with the view hidden.
I leave an image of the menu as is now:
The point is: when Página 2 is active (hidden == NO) the Página 1 button should be without the background.
To few details here, but in the meantime you could play a bit with the following suggestions.
First, move your
[self.view addSubview:_pageOneButton];
outside the method. For example, in viewDidLoad method you add that button to a superview.
Then you can control its state (through a reference to it, as you did) like
// set the image
[_pageOneButton setImage:image forState:UIControlStateNormal];
// remove the image
[_pageOneButton setImage:nil forState:UIControlStateNormal];

Resources