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
}
}
Related
I have created the array of UIButtons , having the same action,
if i click the first button, the first button Colour should be change, other button Colour should not be changed. how to achieve this?
for(titleStr in titleArray){
actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[actionButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
actionButton.tag = count;
[actionButton setTitle:titleArray[count] forState:UIControlStateNormal];
[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change
CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize];
CGRect buttonframe;
buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25);
actionButton.frame = buttonframe;
[titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]];
[contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]];
if(count==0){
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
[actionButton setSelected:YES];
tempObj = actionButton;
}else {
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
}
[titleScrollView addSubview:actionButton];
contentOffset += actionButton.frame.size.width+5.0;
titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height);
// Increase the count value
count++;
}
-(void)buttonTapped:(id)sender {
if([sender tag]==0){
UIButton *tappedButton = (UIButton *)sender;
[actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
actionButton.titleLabel.textColor = [UIColor whiteColor];
[actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
Thanks advance
for that you have to take one extra UIButton Object in you header file.
While user click on any button you have to store that selected Button in refbutton object and use like below.
#interface YourViewController ()
{
UIButton *btnSelected;
}
Now Move to your Button Action:
-(IBAction)buttonTapped:(UIButton *)sender {
if(_btnSelected){
_btnSelected.titleLabel.textColor = [UIColor blackColor];
}
_btnSelected = sender;
sender.titleLabel.textColor = [UIColor whiteColor];
}
Hope this will help you.
I will explain the Scenario:
I am dynamically creating three UIButtons inside a for loop.The condition is by default the first button should be bordered.Now if we select other buttons the previous button's border should go and now the border should be present for selected button.
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
self.graphSubDataButton = [[UIButton alloc] init];
self.graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[self.graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[self.graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
self.graphSubDataButton.tag = btnsCount+1;
[self.graphSubDataButton addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
if (self.graphSubDataButton.tag==1)
{
[self.graphSubDataButton.layer setBorderWidth:1.2f];
[self.graphSubDataButton.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[self.graphSubDataButton.layer setCornerRadius:8.0f];
}
}
-(void) enableRespectiveButton2:(UIButton *) sender
{
}
I have changed your code structure little to make it work properly, you can comment if you don't understand something.
NSInteger defaultSelectedTag = 1;
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
UIButton *graphSubDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
graphSubDataButton.tag = btnsCount+1;
[graphSubDataButton addTarget:self action:#selector(enableRespectiveButton:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
//add your button somewhere
//e.g.
//[self.view addSubview:graphSubDataButton];
if (graphSubDataButton.tag == defaultSelectedTag) {
[self setBorderForButton:graphSubDataButton];
}
}
- (void) setBorderForButton:(UIButton *)sender {
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[sender.layer setCornerRadius:8.0f];
}
- (void) enableRespectiveButton2:(UIButton *) sender {
for(UIButton *buttons in [sender.superview subviews]) {
if([buttons isKindOfClass:[UIButton class]]) {
if(![buttons isEqual:sender]) {
//add logic to, remove borders
//buttons.layer.borderWidth = 0.0f;
}
}
}
[self setBorderForButton:sender];
}
Use tag to get current selected button or much simple: iterate through all subviews and set border to 0 for UIButtons
for (UIView *subview in [uiv_ButtonsView subviews]) {
if([subview isKindOfClass:[UIButton class]]) {
subview.layer setBorderWidth:0.0f;
}
}
then use:
UIButton *button=(UIButton *)sender;
[button.layer setBorderWidth:1.2f];
[button.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[button.layer setCornerRadius:8.0f];
Create dynamic buttons like :
int buttonsOffset = 50;
for (int btnsCount = 0 ; btnsCount <3; btnsCount++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor blueColor];
btn.frame = CGRectMake(buttonsOffset, 100, 120, 40);
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
btn.tag = btnsCount+1;
[btn addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + btn.frame.size.width;
if (btn.tag==1)
{
[btn.layer setBorderWidth:1.2f];
[btn.layer setBorderColor:[UIColor greenColor].CGColor];
[btn.layer setCornerRadius:8.0f];
self.graphSubDataButton = btn;
}
[self.view addSubview:btn];
}
And click event of button is as following
- (void)enableRespectiveButton2:(UIButton *)sender
{
sender.selected = TRUE;
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColor greenColor].CGColor];
[sender.layer setCornerRadius:8.0f];
self.graphSubDataButton.selected = FALSE;
[self.graphSubDataButton.layer setBorderWidth:0.0f];
[self.graphSubDataButton.layer setBorderColor:[UIColor clearColor].CGColor];
[self.graphSubDataButton.layer setCornerRadius:0.0f];
self.graphSubDataButton = sender;
}
Create dynamic buttons with for loop as shown below.
for(int i = 0 ;i <3; i++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
NSString * title = [NSString stringWithFormat:#"Button %d",i];
[btn setTitle:title forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"BorderImg"] forState:UIControlStateSelected];
[btn setBackgroundImage:[UIImage imageNamed:#"UnBordered"] forState:UIControlStateNormal];
if(i == 0) [btn setSelected:YES];
else [btn setSelected:NO];
[btn setTag:i];
[btn addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
}
Have 2 images
Highlighted border (BorderImg)
Normal border (UnBordered)
Set those images to button while creating in for loop
- (void)btnTapped:(UIButton *) btn {
if(! btn.isSelected) {
[btn setSelected:YES];
}
Inside button selector method, based on the sender selection state , update the button selected state. (In side selector method i.e btnTapped set other buttons selected state to false)
I want to perform three different actions for a single button. I surfed in google also it shows some different options but those are not helpful.
I have declared NSInteger inside CustomUIButton i.e. click and initialize inside IBAction to 0.
Here is the code.
-(void)changeStudentAction:(id)sender
{
CustomUIButton *button=sender;
NSInteger flag = 0;
button.click=0;
if(button.click==0)
{
button.click=1;
[button setSelected:NO];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
flag=0;
}
else if (button.click==1)
{
button.click=2;
[button setSelected:YES];
[button setBackgroundImage:[UIImage imageNamed:#"Present_on_coloured.png"] forState:UIControlStateNormal];
flag=1;
}
else if (button.click==3)
{
button.click=0;
[button setBackgroundImage:[UIImage imageNamed:#"Late_on_coloured.png"] forState:UIControlStateNormal];
[button setSelected:YES];
flag=3;
}
}
But it is not working. Can someone tell me where I am doing wrong.
Define button.click=0; globally so initially its value will be zero and after that it will increase.
#interface yourclass : superclass
{
int click;
}
- (void)viewDidLoad{
click=0;
}
-(void)changeStudentAction:(id)sender
{
CustomUIButton *button=sender;
NSInteger flag = 0;
if(click==0)
{
click=1;
[button setSelected:NO];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
flag=0;
}
else if (button.click==1)
{
click=2;
[button setSelected:YES];
[button setBackgroundImage:[UIImage imageNamed:#"Present_on_coloured.png"] forState:UIControlStateNormal];
flag=1;
}
else if (button.click==3)
{
click=0;
[button setBackgroundImage:[UIImage imageNamed:#"Late_on_coloured.png"] forState:UIControlStateNormal];
[button setSelected:YES];
flag=3;
}
}
You are setting the button.click = 0 for every action.
So every time you click it is being set to zero.
You can instead keep a separate variable to count in your view controller than having a click property on button. Hope this helps :-)
According to your code,
it was always goes on
if(button.click==0)
{
button.click=1;
[button setSelected:NO];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
flag=0;
}
I will give you better suggestion instead of "click" you can use "tag" option which was the one property of uibutton.
First you have to define a custom button on .h file and then set the tag = 0 in - (void)viewDidLoad
method.
CustomUIButton *button;
NSInteger flag = 0;
button.click=0;
use it this way or as simple from all such problems use some other variable as
because every time this function is call its zero every time so make it outside this function and use it
NSInteger flag = 0;
int counter = 0;
-(void)changeStudentAction:(id)sender
{
if(counter ==0)
{
counter =1;
[button setSelected:NO];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
flag=0;
}
else if (counter ==1)
{
counter =2;
[button setSelected:YES];
[button setBackgroundImage:[UIImage imageNamed:#"Present_on_coloured.png"] forState:UIControlStateNormal];
flag=1;
}
else if (counter ==3)
{
counter =0;
[button setBackgroundImage:[UIImage imageNamed:#"Late_on_coloured.png"] forState:UIControlStateNormal];
[button setSelected:YES];
flag=3;
}
}
in start button tag to zero then change as following
-(void)changeStudentAction:(id)sender{
CustomUIButton *button=sender;
NSInteger flag = 0;
if(button.tag==0)
{
button.tag=1;
}
else if (button.tag==1)
{
button.tag=2;
}
else if (button.click==3)
{
button.tag=0;
}
}
Please try following code may be solve your problem.
-(void)changeStudentAction:(id)sender
{
CustomUIButton *button=sender;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];
[button setBackgroundImage:[UIImage imageNamed:#"Absent_on_coloured.png"] forState:UIControlStateNormal];
[UIView setAnimationDuration:0.4];
[button setBackgroundImage:[UIImage imageNamed:#"Present_on_coloured.png"] forState:UIControlStateNormal];
[UIView setAnimationDuration:0.6];
[button setBackgroundImage:[UIImage imageNamed:#"Late_on_coloured.png"] forState:UIControlStateNormal];
[UIView commitAnimations];
}
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];
}
}
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.