iOS how do I reset button images? - ios

I have a few buttons that when pressed they change from a plain button to a button with a picture of a tick. The problem I have is that I can't then get a ticked button back to an untucked button when a separate button is pressed. Is this possible at all?
Here are my buttons:
- (IBAction)pickCat:(UIButton *)sender {
[self deselectAllButtons];
((UIButton *)sender).selected = true;
UIImage *btnImage1 = [UIImage imageNamed:#"white - on.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
self.catLabel.text = #"WHITE (Hi-Po)";
}
- (IBAction)pickCatb:(UIButton *)sender {
[self deselectAllButtons];
((UIButton *)sender).selected = true;
UIImage *btnImage1 = [UIImage imageNamed:#"red - on.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
self.catLabel.text = #"RED (Significant)";
}
- (IBAction)pickCatc:(id)sender {
UIImage *btnImage1 = [UIImage imageNamed:#"yellow - on.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
self.catLabel.text = #"YELLOW (Serious)";
}
- (IBAction)pickCatd:(id)sender {
UIImage *btnImage1 = [UIImage imageNamed:#"green - on.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
self.catLabel.text = #"GREEN (Important)";
}
- (void)deselectAllButtons
{
// assuming you have a reference to all your buttons
self->Btn1.selected = false;
self->Btn2.selected = false;
}

I like to set a different image for each state: normal and selected. It looks like you're using Interface Builder, and it's really easy to set up your buttons with different images for a normal and a selected state.
Then in your code you can do something like this:
- (IBAction)pickImage:(id)sender {
[self deselectAllButtons];
sender.selected = true;
self.locLabel.text = #"Red";
}
- (IBAction)pickImageb:(id)sender {
[self deselectAllButtons];
sender.selected = true;
self.locLabel.text = #"Red";
}
- (void)deselectAllButtons
{
// assuming you have a reference to all your buttons
self.btn1.selected = false;
self.btn2.selected = false;
...
}
If you want to be able to reset all buttons before changing one to a checked image, you have to have a reference to everything else you want to affect. If you want to get fancy, look into Outlet Collections so you can easily loop through an array of buttons from your IB file and set their selected state to false.
UPDATE:
Here's how your code should probably look:
- (IBAction)pickCat:(UIButton *)sender {
[self deselectAllButtons];
// no need to cast since you changed your method signature.
// the system now expects sender to be a button
// ((UIButton *)sender).selected = true;
sender.selected = true;
// no need to set the image explicitly. if you did things correctly
// in IB, you've already told the button which image to use when
// selected, and which image to use when not (default). when you set
// the line above (sender.selected = true), that will tell the button
// to go use the image that you added to the button's selected state in IB
// UIImage *btnImage1 = [UIImage imageNamed:#"white - on.png"];
// [sender setImage:btnImage1 forState:UIControlStateNormal];
self.catLabel.text = #"WHITE (Hi-Po)";
}
- (IBAction)pickCatb:(UIButton *)sender {
[self deselectAllButtons];
sender.selected = true;
self.catLabel.text = #"RED (Significant)";
}
- (IBAction)pickCatc:(id)sender {
[self deselectAllButtons];
sender.selected = true;
self.catLabel.text = #"YELLOW (Serious)";
}
- (IBAction)pickCatd:(id)sender {
[self deselectAllButtons];
sender.selected = true;
self.catLabel.text = #"GREEN (Important)";
}
- (void)deselectAllButtons
{
// 2 things:
// 1) I'm not sure about self->Btn1. why not self.Btn1?
// 2) it looks like you have 4 buttons (catA, catB, catC, catD). if so, you need to set the selected state for all of them to false
self->Btn1.selected = false;
self->Btn2.selected = false;
self->Btn3.selected = false;
self->Btn4.selected = false;
}

Related

Change the background color of selected TextField

I am creating an app in which I have used user registration.In registration page, I have created many UITextFields.I wants that when a user select any TextField then its background Image is change. I uses the code to do that is:
- (IBAction)touchBegin:(id)sender
{
//UILabel *opt = (UILabel *)[cell viewWithTag:101];
if (_text1.isSelected) {
_text1.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text2.isSelected){
_text2.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text3.isSelected){
_text3.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else if (_text4.isSelected){
_text4.background = [UIImage imageNamed:#"big_star_non_rating.png"];
}
else{
}
}
But I am not getting the result according to my need.
What should I use to get correct output?
be sure whether your textfield border style is UITextBorderStyleNone.
yourTextField.borderStyle = UITextBorderStyleNone;
yourTextField.background = [UIImage imageNamed:#"image.png"];
I think that it's better and easier to use UITextFieldDelegate methods like this, for example:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.background = [UIImage imageNamed:#"big_star_non_rating"];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
textField.background = [UIImage imageNamed:#"AnImageForNormalStateOrSetNilBackground"];
return YES;
}
Hope it helps.

Detect Selected State of UIButton iOS

How can I detect the selected state of a uibutton?
I have 7 buttons and I have made them to be able to toggle or select multiple buttons at a time.
I want to be able to tell which buttons are in a selected state when I push the done button.
So if M, T and W are selected then I want to be able to detect that when pushing done.
I currently put a tag on the button and then call a method to unselect or select multiple buttons.
self.repeatOccurrenceFrequencyWeeklyTF = [[UITextField alloc]init];
self.repeatOccurrenceFrequencyWeeklyTF.frame = CGRectMake(80, 80, 32, 32);
self.repeatOccurrenceFrequencyWeeklyTF.delegate = self;
self.repeatOccurrenceFrequencyWeeklyTF.background = [UIImage imageNamed:#"repeatWeekly"];
self.repeatOccurrenceFrequencyWeeklyTF.font = [UIFont fontWithName:#"SegoeWP" size:15];
self.repeatOccurrenceFrequencyWeeklyTF.textColor = [UIColor appGreyText];
[self.repeatOccurrenceFrequencyWeeklyTF setValue:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0] forKeyPath:#"_placeholderLabel.textColor"];
self.repeatOccurrenceFrequencyWeeklyTF.placeholder = #"1";
UIView *leftView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, self.repeatOccurrenceFrequencyWeeklyTF.frame.size.height)];
self.repeatOccurrenceFrequencyWeeklyTF.leftView = leftView1;
self.repeatOccurrenceFrequencyWeeklyTF.leftViewMode = UITextFieldViewModeAlways;
self.repeatOccurrenceFrequencyWeeklyTF.rightViewMode = UITextFieldViewModeAlways;
self.keyboardToolbar = [self createInputToolbar];
self.repeatOccurrenceFrequencyWeeklyTF.inputAccessoryView = self.keyboardToolbar;
self.repeatOccurrenceFrequencyWeeklyTF.delegate = self;
self.repeatOccurrenceFrequencyWeeklyTF.keyboardType = UIKeyboardTypeNumberPad;
self.repeatOccurrenceFrequencyWeeklyTF.enabled = NO;
[self.view addSubview:self.repeatOccurrenceFrequencyWeeklyTF];
// Now, in your button action handler, you can do something like this:
- (void)mondayButtonTouch:(UIButton *)aButton withEvent:(UIEvent *)event
{
aButton.selected = !aButton.selected;
if(aButton.tag == 111) {
}
if(aButton.tag == 222) {
}
if(aButton.tag == 333) {
}
if(aButton.tag == 444) {
}
if(aButton.tag == 555) {
}
if(aButton.tag == 666) {
}
NSLog(#"dsfdfdfsdfs %ld", (long)aButton.tag);
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
}
I would use a NS_ENUM (which helps to keep a nice and readable code) and a NSMutableArray to keep track of your selected buttons.
Declare a enum that looks something like this:
typedef NS_ENUM(NSInteger, Weekday) {
WeekdayMonday,
WeekdayTuesday,
WeekdayWednesday,
WeekdayThursday,
WeekdayFriday,
WeekdaySaturday,
WeekdaySunday
};
Then tag your buttons with the correct enum:
tuesdayButton.tag = WeekdayTuesday;
And check when you tap button if your enum exists in your array:
- (void)buttonTouch:(UIButton *)aButton withEvent:(UIEvent *)event
{
if ([array containsObject:#(aButton.tag)]){ //exists, remove it from array
[array removeObjectIdenticalTo:#(aButton.tag)];
}
}else{
[array addObject:#(aButton.tag)];
}
}
A possibility is to create an NSMutableArray named selectedButton. Do like this:
- (void)mondayButtonTouch:(UIButton *)aButton withEvent:(UIEvent *)event
{
aButton.selected = !aButton.selected;
if(!aButton.selected && selectedButton.containsObject(aButton.Tag)) {
[selectedButton removeObject:aButton.tag];
}
else if(aButton.selected && !selectedButton.containsObject(aButton.Tag)) {
[selectedButton addObject:aButton.tag];
}
// do your stuff here
}
Now on done button click you have all the button thats tags are selected will be trackable with selectedButton array.
You can use:
[self.view viewWithTag:yourTagHere]
you can use this :
for (UIButton *btn in [self.view subviews]) { // self.view (change it with your button superview)
if ([btn isKindOfClass:[UIButton class]] && [btn isSelected] == YES) {
// here you found the button which is selected
}
}
[self.view viewWithTag:yourTagHere] replace

Is my NSMutablearray adding the objects and my NSLog the correct number of objects in the array?

Im working on a project and i need some advice. I have this button handler code that handle all my buttons clicks(like a survey). I want to click one button and have the button add the appropriate object to the NSMutable array. now my question is is it safe to use a mutable array or a dictionary since mutable objects can be added or removed. and how can i check if the objects are being added to the array correctly. i have a NSUInteger to see if the objects are being added correctly but i'm receiving in the NSlog objects in the array 1 and whenever i click another button to add another object i receive the same message but its NSlog prints 1 again so im not sure if the objects are being added correctly or not.
-(IBAction)checkBoxButtonHandler:(id)sender
{// array being allocated
NSMutableArray *arrayOfselections = [NSMutableArray array];
if (sender == checkButton1)
{
if (!buttonChecked)
{
[arrayOfselections addObject:#"Freshman"];
NSLog(#"Freshman Checked");
[checkButton1 setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkButton1 setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
}
if (sender == checkB2)
{
// [arrayOfselections addObject:#"Sophmore"];
// NSLog(#"check 2");
if (!buttonChecked)
{
[arrayOfselections addObject:#"Sophmore"];
NSLog(#"Sophmore Checked");
[checkB2 setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkB2 setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
}
[arrayOfselections accessibilityElementCount];
if (sender == checkBox3)
{
//NSLog(#"check 3");
if (!buttonChecked)
{
[arrayOfselections addObject:#"Junior"];
NSLog(#"Junior Checked");
[checkBox3 setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkBox3 setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
}
if (sender == checkBx4)
{
NSLog(#"Checked 4");
if (!buttonChecked)
{
[arrayOfselections addObject:#"Senior"];
NSLog(#"Senior Checked");
[checkBx4 setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];
buttonChecked = YES;
}
else
{
[checkBx4 setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
buttonChecked = NO;
}
NSUInteger arraySize = [arrayOfselections count];
NSLog(#"Number of stuff inside array: %ui",arraySize);
}
You create new array every time you press the button, this happened in this line:
NSMutableArray *arrayOfselections = [NSMutableArray array];
You need to create it just once. The best way is just move it to viewDidLoad method, it should do the job.
You need to make the array a property of the view controller:
MyViewController.m:
#interface MyViewController ()
#property (nonatomic) NSMutableArray *arrayOfSelections;
#end
Which is initialized in viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
self.arrayOfSelections = [NSMutableArray new];
...
}
-(IBAction)checkBoxButtonHandler:(id)sender
{
// Remove this line:
//NSMutableArray *arrayOfselections = [NSMutableArray array];
...
// and change any references to self.arrayOfSelections:
[self.arrayOfselections addObject:#"Freshman"];
}
A prettier design for this would be to use tags and a dictionary instead of if-else, so
// assign tags:
checkButton1.tag = 1;
checkButton2.tag = 2;
// etc., then setup data beforehand
NSDictionary *mapTags = #{ #1:#"Freshman", #2:#"Sophomore" /* etc */ };
NSMutableArray *arrayOfselections = [NSMutableArray array];
// then look how small this gets:
-(IBAction)checkBoxButtonHandler:(id)sender {
NSNumber *tagNumber = [NSNumber numberWithInt:sender.tag];
[self.arrayOfselections addObject:self.mapTags[tagNumber]];
}

Changing a UIButton to be turned off

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

Toggle the button Image

After i implemented a code for give an check image for a button but i want to give check image for no of buttons.
note: if i click on one button check image can be displayed and the same time i click on another button check image displayed on that particular button and previous button comes normal position.
i implement the code for single button here like this.
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"btn_check_on.png"];
[self setImage:img forState:UIControlStateNormal];
}
else
{
UIImage* img = [UIImage imageNamed:#"bread_Wheat_rectangle.png"];
[self setImage:img forState:UIControlStateNormal];
}
}
The above code is executed successfully but how to use this code for no of buttons.
please suggest any tutorial regarding my problem
This is how I have implemented it for one button. You can use it for more buttons too.
-(IBAction)ButtonAction
{
if (Flag==0)
{
Flag=1;
[myButton setImage:[UIImage imageNamed:#"checkbox-filled.png"] forState:UIControlStateNormal];
}
else
{
[myButton setImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
Flag=0;
}
}
Note : If you want only one button to get checked Just set all other buttons image as checkbox.png and the selected one's checkbox-filled.png.
EDIT
You can make a class for checkbox and then use it. Here is the code...
CheckButton.h
#import <Foundation/Foundation.h>
#interface CheckButton : UIButton {
BOOL _checked;
int chkButtonClickVal;
}
#property (nonatomic, setter=setChecked:) BOOL checked;
-(void) setChecked:(BOOL) check;
-(int)chkButtonClickVal;
#end
CheckButton.m
#import "CheckButton.h"
#implementation CheckButton
#synthesize checked = _checked;
-(id) init
{
if( self=[super init] )
{
chkButtonClickVal=0;
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(void) awakeFromNib
{
self.checked = NO;
[self addTarget:self action:#selector(OnCheck:) forControlEvents:UIControlEventTouchUpInside];
}
-(void) dealloc
{
chkButtonClickVal=0;
[super dealloc];
}
-(void) setChecked:(BOOL) check
{
_checked = check;
if( _checked )
{
UIImage* img = [UIImage imageNamed:#"checkbox-checked.png"];
[self setImage:img forState:UIControlStateNormal];
chkButtonClickVal=1;
}
else
{
UIImage* img = [UIImage imageNamed:#"checkbox.png"];
[self setImage:img forState:UIControlStateNormal];
chkButtonClickVal=2;
}
//NSLog(#"%d",chkButtonClickVal);
}
-(int)chkButtonClickVal
{
return chkButtonClickVal;
}
-(void) OnCheck:(id) sender
{
self.checked = !_checked;
}
#end
I have done it in same way. Try you'll be able to achieve it.
Good Luck :)
After long practicing this problem we can use switch cases it can be done very easily
switch (currenttagvalue) {
case 1:
[level1 setImage:[UIImage imageNamed:#"one_time_selected.png"] forState:UIControlStateNormal];
[level2 setImage:[UIImage imageNamed:#"ic_launcher.png"] forState:UIControlStateNormal];
[level3 setImage:[UIImage imageNamed:#"bread_sourdough_rectangle.png"] forState:UIControlStateNormal];
}
in this level is "IBOutlet UIButton level1;"
And then i implement so more buttons

Resources