UIButton like Rating bar that i want to do anyone help me? - ios

i want to use button is in either way
i want to use if user select one time button then its display different color after that click on button it appear as usual after click it display that color
static int tapCount = 0;
- (IBAction)BtnClickdemo:(id)sender
{
tapCount++;
if (tapCount == 1)
[btnClick setImage:[UIImage imageNamed:#"line.png"] forState:UIControlStateNormal];
else if (tapCount == 0)
[btnClick setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
else
{
[btnClick setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
}
}
User only select 2 tappded like Ratingbar taped ??
How can i do ??

You can assign a BOOL value to your button and check this value everytime the button is tapped:
#implementation youCustomViewController {
BOOL buttonSelectionned;
UIButton *myButton;
}
- (void)viewDidLoad
{
/* Init objects */
buttonSelectionned = NO;
[super viewDidLoad];
// Do any additional setup after loading the view.
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100);
myButton setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
and then implement your method buttonTapped
- (void)buttonTapped:(id)sender
{
if (buttonSelectionned) {
buttonSelectionned = NO;
myButton setImage:[UIImage imageNamed:#"MinSelected.png"] forState:UIControlStateNormal];
} else {
buttonSelectionned = YES;
myButton setImage:[UIImage imageNamed:#"MinSelected2.png"] forState:UIControlStateNormal];
}
}

Related

UIbutton does not reset to default after click on Reset button

I have a product filter list and I would like to reset all data to default (without click) after clicked on Reset button.
But now my case is after I clicked on Reset button, selected items will not reset accordingly and it will change to other items. You may refer to screenshot as below:-
Here is my part of related code, please help and thanks for your advice:-
FilterItemCell.m
- (void)setUpUI
{
_contentButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_contentButton setTitleColor: ThemeBlueColor
forState:UIControlStateNormal];
[_contentButton addTarget:self action:#selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_contentButton];
}
- (void)checkButtonTapped:(id)sender
{
if ([sender isSelected]) {
!_attributeItemClick ? : _attributeItemClick();
[sender setSelected: NO];
[_contentButton setImage:nil forState:0];
[_contentButton setTitleColor:ThemeBlueColor forState:UIControlStateNormal];
} else {
!_attributeItemClick ? : _attributeItemClick();
[sender setSelected: YES];
[_contentButton setImage:[UIImage imageNamed:#"isSelectYes"] forState:0];
[_contentButton setTitleColor:ThemeRedColor forState:UIControlStateNormal];
}
}
Filter_ViewController.m
- (void)setUpBottomButton
{
CGFloat buttonW = FiltrateViewScreenW/2;
CGFloat buttonH = 40;
CGFloat buttonY = ScreenH -10 - buttonH;
NSArray *titles = #[#"Reset",#"OK"];
for (NSInteger i = 0; i < titles.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:titles[i] forState:UIControlStateNormal];
button.tag = i;
CGFloat buttonX = i*buttonW;
button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
button.titleLabel.font = PFR16Font;
if (i == 0)
{
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"Button2-1"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(bottomButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
else if (i == 1)
{
[button setBackgroundImage:[UIImage imageNamed:#"Button1-1"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(bottomButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
}
[_filtrateConView addSubview:button];
}
}
- (void)bottomButtonClick:(UIButton *)button
{
if (button.tag == 0) //Reset Button
{
//CURRENT CODE TO RELOAD COLLECTION VIEW DATA
[self.collectionView reloadData];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FilterItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FilterItemCellID forIndexPath:indexPath];
cell.attribute_name=[[self.filterItem[indexPath.section] valueForKey:#"attribute_name"]objectAtIndex:indexPath.row];
return cell;
}
for reset function you need to renew all the data as in starting like you might be storing the ticked ones in an array.
you need to refresh the array you are using to display in your collectionView.
Then you need to call reload on your collectionView.
In your function I can see only reload function call not any function for refreshing the data in the array you might be using to display in the collection view.
I hope this helps you. Do let me know in case you need anything else.

CheckBox issues with images

When I select the check box the second time it shows me the checked image, but the status is unchecked.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
[CheckBox setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateNormal];
}
- (IBAction)CheckBox:(id)sender {
if (CheckBox.selected == NO)
{
CheckBox.selected = YES;
[CheckBox setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateNormal];
}
else
{
CheckBox.selected = NO;
[CheckBox setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateSelected];
}
}
Your code is totally right just you replace UIControlStateSelected to UIControlStateNormal.
You need add normal image and selected images in IB itself (or in viewDidLoad) and toggle state for button in button action like below
- (IBAction)checkBoxAction:(UIButton *)sender {
sender.selected = !sender.selected;
}
if (sender.tag==0)
{
sender.tag = 1;
[sender setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
else
{
sender.tag=0;
[sender setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}
When create button for check box you can save address this element on array after click on every button first access to all object on array seved and set image unckeck and this button clicked set image checked.
Or
When create button for check box set tag different for every button and when click on button you should be set image unckeck for every button use tag number and set image check for button clicked.
Also one thing,
Here you use CheckBox.selected = YES/NO;
So also you can write your code like:
- (void)viewDidLoad {
[super viewDidLoad];
CheckBox.selected = NO;
[CheckBox setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateNormal];
[CheckBox setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateSelected];
}
- (IBAction)CheckBox:(id)sender {
if (CheckBox.selected == NO)
{
CheckBox.selected = YES;
}
else
{
CheckBox.selected = NO;
}
}
- (IBAction)changeState:(id)sender {
UIButton *btn=(UIButton *)sender;
if (btn.selected == NO)
{
btn.selected = YES;
[btn setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateNormal];
}
else
{
btn.selected = NO;
[btn setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateNormal];
}
}
You should set boolean value to false in viewDidLoad and then in button click
- (IBAction)btnCheckBoxClick:(id)sender
{
if (isChecked)
{
[btnCheckBox setImage:[UIImage imageNamed:#"btncheckbox.png"] forState:UIControlStateNormal];
isChecked = false;
}
else
{
[btnCheckBox setImage:[UIImage imageNamed:#"btncheckbox_selected.png"] forState:UIControlStateNormal];
isChecked = true;
}
}

Is there a way of doing an action locally on a UIButtton?

I am setting up a table with an I have this code:
UIButton *done = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)];
[done setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[done addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:done];
return cell;
This doesn't work beacuse this button is only declared in this scope and I want to simply change its image on this click
Change the image in the done: method.
- (void)done:(id)sender
{
UIButton *button = (UIButton *)sender;
[button setImage:[UIImage imageNamed:#"yourImage.png"]
forState:UIControlStateNormal];
}
What does you done method look like?
I think that if you add a sender argument to the method, you can access the button.
- (void)done:(id)sender
{
UIButton *button = (UIButton*)sender;
[button doStuff];
}
You have to declare an ivar or a private/public property. So you can change its image from everywhere, for example:
#interface ViewController ()
#property UIButton *done;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.done = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)];
[self.done setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[self.done addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.done];
}
- (void)done:(UIButton *)sender
{
//Do something.. like change image
[self.done setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
#end
Or you declare the sender as the UIButton, like this:
- (void)done:(id)sender
{
UIButton *done = (UIButton *)sender;
//Do something with the button
}

how to select all inside IBOutletCollection

I created a IBOutletCollection property and action for button1 to button9. Each and every button is pressed color will change,
-(IBAction)btnCollectionAction:(id)sender {
counter = 0;
btnSelecter = sender;
if (counter == 0) {
[btnSelecter setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
counter = 1;
}
else {
[btnSelecter setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
counter = 0;
}
}
IBOutlet for selectall button,
- (IBAction)selectButtonFunction1:(id)sender {
if (counter == 0) {
[selBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
counter = 1;
}
else {
[selBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
counter = 0;
}
}
the above both script works well.
My question is - if i pressed the select all button (change to whiteColor), the above all nine button must change to whiteColor else blackColor.
I tried like this:
if(selBtn1.touchInside) {
[btnSelecter setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
else {
[btnSelecter setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
it will change only one button instead of all.
Last part of your question is vague, I didn't understand the last portion. From your title I understand that you need to get all the UIButton from IBOutletCollection. I'm answering for that question.
You can toggle the color like:
- (IBAction)selectAll:(id)sender
{
for (UIButton *button in buttonCollection)
{
if([button titleColorForState:UIControlStateNormal] isEqual:[UIColor blackColor]])
{
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
else
{
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
}
}
Try this approach:
1. Create IBOutletCollection with all your buttons (You Allready done this)
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *myButtons;
2. Iterate throughout collection to configure target and appearence for each button:
for (UIButton *button in self.myButtons)
{
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
// Do any additional configuration here
}
3. Add target to selectAll button and assign Tag property for selectAll button
[selecAllButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
selectAllButton.tag = 10000; // you can other tag number
4. Create buttonPressed: Handler
-(void) buttonPressed:(UIButton *)sender
{
if(sender.tag = 10000)
{
for (UIButton *button in self.menuButtons) {
if([button isKindOfClass:[UIButton class]]){
button.selected = YES;
}
}
} else
{
// handle other button actions
}
}

iOS UIButton disappears after setting background image to nil

I have a UIButton that has no title or initial background image. When pressed, the button background image changes and when pressed again, it is set to nil. Now with iOS 6.x, the button completely disappears. Here is my code:
- (IBAction)resetAll: (id) sender {
[self reset];
for (UIView *view in self.view.subviews) {
if ([[[view class] description] isEqualToString:#"UIRoundedRectButton"]) {
UIButton *theButton = (UIButton *)view;
int nTag = theButton.tag;
switch (nTag) {
case 11: {
theButton.tag = 10;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
break;
}
case 21: {
theButton.tag = 20;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
case 31: {
theButton.tag = 30;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
case 41: {
theButton.tag = 40;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
case 51: {
theButton.tag = 50;
[theButton setBackgroundImage:nil forState:UIControlStateNormal];
[theButton setEnabled:YES];
break;
}
}
}
}
return;
This code works fine:
- (IBAction)ButtonClicked: (id) sender {
UIButton *btn = (UIButton *)sender;
// Get the current tag value
int currentTag = [sender tag];
// Toggle the check buttons
if (currentTag == 10 || currentTag == 20 || currentTag == 30 || currentTag == 40 || currentTag == 50) {
[btn setBackgroundImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
btn.tag = ++currentTag;
}
else
if (currentTag == 11 || currentTag == 21 || currentTag == 31 || currentTag == 41 || currentTag == 51) {
[btn setBackgroundImage:nil forState:UIControlStateNormal];
btn.tag = --currentTag;
}
[self calculate];
Any suggestions or help would be appreciated!
Instead of changing the background image of the button in the UIControlStateNormal, why not instead just change states of the button?
If you created the button programmatically, you just need to add the line
[btn setBackgroundImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateSelected];
Then, when pressed and you want it to be the arrow image:
[btn setSelected:YES];
and to set it back to the default appearance:
[btn setSelected:NO];
If you made the button in a XIB, you can set the state images by changing the state config to Selected and set the image there.
Here is the final code implementing DOC's answer. I set the default and selected state in interface builder:
- (IBAction)ButtonClicked: (id) sender {
UIButton *btn = (UIButton *)sender;
if ([btn isSelected ]) {
[btn setSelected:NO];
} else {
[btn setSelected:YES];
}
[self calculate];
}
- (IBAction)resetAll: (id) sender {
[self reset];
for (UIView *view in self.view.subviews) {
if ([[[view class] description] isEqualToString:#"UIRoundedRectButton"]) {
UIButton *theButton = (UIButton *)view;
[theButton setSelected:NO];
}
}
Just an additional answer in case (like me) there is already a good amount of logic around the selected state of the button (parent class gives state to the view based on this). If you need the button to start in an un-selected state with an image and change state to selected with the no image, then you can add a generated blank image to the selected state like so.
//Make a stupid blank image because setting to nil on a state doesn't work.
UIGraphicsBeginImageContextWithOptions(CGSizeMake(23, 23), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *buttonNonSelectedImage = [UIImage imageNamed:#"video-slomo"];
[self.myButton setBackgroundImage:buttonNonSelectedImage forState:UIControlStateNormal];
[self.mybutton setBackgroundImage:blank forState:UIControlStateSelected];
[self.myButton addTarget:self
action:#selector(onMyButtonPress)
forControlEvents:UIControlEventTouchUpInside];
self.myButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[self.playRateButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[self.view addSubview:self.myButton];
Then, in your button action you can just change the state (and in my case add text).
- (void)onMyButtonPress
{
if (self.myButton.isSelected) {
[self.myButton setSelected:NO];
}
else {
[self.myButton setTitle:#"Selected"]
forState:UIControlStateSelected];
[self.myButton setSelected:YES];
}
}
The accepted answer works in most cases, I just needed the selected state for other tracking info. Happy programming.

Resources