UIButton pressed, other one doesn't press - ios

hi I have some code that creates two buttons and when a button is pressed an trigger is set and it changes the background and the text colour.
How could i make it so that once one is pressed the other cannot be pressed.
This creates the buttons
//learnmore button
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:learnmorebutton];
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:#"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
//signup button
UIButton *signup = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:signup];
signup.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
signup.frame = CGRectMake(162.0, 518.0, 160.0, 50.0);
[signup setTitle:#"sign up" forState:UIControlStateNormal];
signup.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
signup.titleLabel.textColor = [UIColor whiteColor];
These are the two triggers
[learnmorebutton addTarget:self action:#selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];
[signup addTarget:self action:#selector(signupClickEvent:) forControlEvents:UIControlEventTouchUpInside];
These are the two events
- (void)learnMoreClickEvent:(UIButton *)sender
{
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
- (void)signupClickEvent:(UIButton *)sender
{
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
thanks all ...

You start by keep a reference to the button, preferably properties on the viewController that creates these buttons.
#interface MyViewController : UIViewController
#property (nonatomic, strong) UIButton *learnmorebutton;
#property (nonatomic, strong) UIButton *signup;
#end;
Then in the method either disable the button:
<button>.enabled = NO:
Or hide it:
<button>.hidden = YES:

like rckoenes mentioned you can declare those UIButton as properties and do that.. or
Assign Tags like this
//learnmore button
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
learnmorebutton.tag = 10;
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:#"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
[learnmorebutton addTarget:self action:#selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:learnmorebutton];
//signup button
UIButton *signup = [UIButton buttonWithType:UIButtonTypeRoundedRect];
signup.tag = 20;
signup.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
signup.frame = CGRectMake(162.0, 518.0, 160.0, 50.0);
[signup setTitle:#"sign up" forState:UIControlStateNormal];
signup.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
signup.titleLabel.textColor = [UIColor whiteColor];
[self.view addSubview:signup];
[signup addTarget:self action:#selector(signupClickEvent:) forControlEvents:UIControlEventTouchUpInside];
By Using tags to access the buttons
- (void)learnMoreClickEvent:(UIButton *)sender
{
UIButton *signUp = (UIButton*)[self.view viewWithTag:20]
signUp.enabled = NO;
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
- (void)signupClickEvent:(UIButton *)sender
{
UIButton *learnmorebutton = (UIButton*)[self.view viewWithTag:10]
learnmorebutton.enabled = NO;
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
Hope this gives you an Idea.
Basically if your looking for toggling, you can always use UISegementControl. I mean it depends on your requirements.

Set the enabled property of the button you want to disable to NO: <button>.enabled = NO;

Related

UIButton set title color not working

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.

iOS bordered button behaviour on tap

I need a pre-iOS UIButton with border in my app. I use:
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 10;
This works ok but on tap the border does not flash(button highlight state). Any way to easily implement this ?
Implement this when creating the button:
[self.botonConfigurar addTarget:self action:#selector(setBgColorForButton:) forControlEvents:UIControlEventTouchDown];
[self.botonConfigurar addTarget:self action:#selector(clearBgColorForButton:) forControlEvents:UIControlEventTouchDragExit];
[self.botonConfigurar addTarget:self action:#selector(setBgColorForButton:) forControlEvents:UIControlEventTouchDragEnter];
and these 2 functions:
-(void)setBgColorForButton:(UIButton*)sender {
[self.botonConfigurar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.botonConfigurar.layer.backgroundColor = [UIColor colorWithRed:89/255.0 green:194/255.0 blue:237/255.0 alpha:1.0].CGColor;
}
-(void)clearBgColorForButton:(UIButton*)sender {
[self.botonConfigurar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.botonConfigurar.layer.backgroundColor = [UIColor colorWithRed:61/255.0 green:169/255.0 blue:222/255.0 alpha:1].CGColor;
}
Do whatever you want with the button inside these functions.
PD: Make your button a [UIButton buttonWithType:UIButtonTypeCustom]
PD2: In clearBgColorForButton:sender write your UIButton's original parameters.
This is Just Example
[button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateHighlighted];

UIButton text of label not visible

Problem: If the user goes on the iPhone/iPad to "Settings - General - Accessibility - Increase Contrast" and switches on "Darken Colors" the text from my buttons disappears.
Here is my code for creating the buttons:
buttonCurrent = [UIButton buttonWithType:UIButtonTypeSystem];
buttonCurrent.translatesAutoresizingMaskIntoConstraints = NO;
buttonCurrent.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
buttonCurrent.titleLabel.textAlignment = NSTextAlignmentCenter;
buttonCurrent.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonCurrent.titleLabel.numberOfLines = 2;
buttonCurrent.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
buttonCurrent.backgroundColor = [UIColor whiteColor];
[buttonCurrent setTitle:NSLocalizedString(#"Add the reading with the current date", nil) forState:UIControlStateNormal];
[buttonCurrent addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Thanks for the help.
I changed the code to:
buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCurrent.translatesAutoresizingMaskIntoConstraints = NO;
buttonCurrent.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
[buttonCurrent setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[buttonCurrent setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[buttonCurrent setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
buttonCurrent.titleLabel.textAlignment = NSTextAlignmentCenter;
buttonCurrent.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonCurrent.titleLabel.numberOfLines = 2;
buttonCurrent.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
buttonCurrent.backgroundColor = [UIColor grayColor];
[buttonCurrent setTitle:NSLocalizedString(#"Add the reading with the current date", nil) forState:UIControlStateNormal];
It looks now like the UIButtonTypeSystem and it works also with system setting "Darken Colors".
all code is fine except first line.
buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
UIButtonTypeSystem have default blue color as a back ground in greater than iOS7. You should always use UIButtonTypeCustom, so that it does not shows the effect as you are experience.
And also please give frame. I checked your code and you have not given frame for button. Please do that first.
UIButton *buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCurrent.translatesAutoresizingMaskIntoConstraints = NO;
buttonCurrent.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
buttonCurrent.titleLabel.textAlignment = NSTextAlignmentCenter;
buttonCurrent.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonCurrent.titleLabel.numberOfLines = 2;
buttonCurrent.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
buttonCurrent.backgroundColor = [UIColor whiteColor];
buttonCurrent.frame = CGRectMake(0, 50, 320, 80);
[buttonCurrent setTitleColor:[UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
[buttonCurrent setTitle:#"Add the reading with the current date" forState:UIControlStateNormal];
[buttonCurrent addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonCurrent];
All code is fine except first line
buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
UIButtonTypeSystem have default blue color as a back ground in greater than iOS7. You should always use UIButtonTypeCustom, so that it does not shows the effect as you are experience.

programmatically adding custom button messing with eachother

I have a void I'm calling that shows hidden view on a cell when i swipe over, when they were al set to icons the positioning worked great, however changing them to custom buttons so i can have text instead of icons, had messed things up.
EDIT: also, if i touch where the buttons SHOULD be, it works as if there really there, it just "Looks" messed up. For example, if in the example where its wrong (image) how the decline1 is on the left, if i click it, it accepts the request, but if i click in the middle empty area, nothing.
This is the coding
- (void)bottomDrawerWillAppear {
UIImageView *drawerBGImg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,75)];
NSString *friendAvatar = [NSString stringWithFormat:#"%#%#%#", #"http://www.thatonewebsite.com/images/users/", [MyClass friendID], #".jpg"];
[drawerBGImg setImageWithURL:[NSURL URLWithString:friendAvatar]];
self.bottomDrawer.clipsToBounds = YES;
drawerBGImg.contentMode = UIViewContentModeScaleAspectFill;
[self.bottomDrawer addSubview:drawerBGImg];
UIImageView *drawerBG = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,75)];
drawerBG.image = [UIImage imageNamed:#"drawerBG.png"];
[self.bottomDrawer addSubview:drawerBG];
NSLog(#"%#", [MyClass friendID]);
UIButton *inviteToLocationBtn=[UIButton buttonWithType:UIButtonTypeCustom];
inviteToLocationBtn.frame=CGRectMake(15.0, 15.0, 80.0, 50.0);
[inviteToLocationBtn setTitle:#"accept" forState:UIControlStateNormal];
inviteToLocationBtn.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size: 22.0f];
inviteToLocationBtn.titleLabel.textColor = [UIColor whiteColor];
[inviteToLocationBtn addTarget:self action:#selector(callAccept) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:inviteToLocationBtn];
UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame=CGRectMake(130.0, 15.0, 80.0, 50.0);
[inviteToLocationBtn setTitle:#"decline1" forState:UIControlStateNormal];
inviteToLocationBtn.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size: 22.0f];
inviteToLocationBtn.titleLabel.textColor = [UIColor whiteColor];
[deleteBtn addTarget:self action:#selector(callDeny) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:deleteBtn];
UIButton *messageBtn=[UIButton buttonWithType:UIBarButtonSystemItemCompose];
messageBtn.frame=CGRectMake(245.0, 15.0, 50.0, 50.0);
UIImage *messageImage = [UIImage imageNamed:#"messageIcon.png"];
[messageBtn setImage:messageImage forState:UIControlStateNormal];
[messageBtn addTarget:self action:#selector(callChat) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:messageBtn];
}
before you take a look, i apologize for the large images
this is how it looks
This is how its supposed to look, The middle image, invitation.
After you declare deleteBtn you set the title as #"decline1" but call inviteToLocationBtn. [inviteToLocationBtn setTitle:#"decline1" forState:UIControlStateNormal] should refer to deleteBtn.
this line above:
UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame=CGRectMake(130.0, 15.0, 80.0, 50.0);
[inviteToLocationBtn setTitle:#"decline1" forState:UIControlStateNormal];
inviteToLocationBtn.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size: 22.0f];
inviteToLocationBtn.titleLabel.textColor = [UIColor whiteColor];
[deleteBtn addTarget:self action:#selector(callDeny) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:deleteBtn];
should be:
UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame=CGRectMake(130.0, 15.0, 80.0, 50.0);
[deleteBtn setTitle:#"decline1" forState:UIControlStateNormal];
deleteBtn.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size: 22.0f];
deleteBtn.titleLabel.textColor = [UIColor whiteColor];
[deleteBtn addTarget:self action:#selector(callDeny) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:deleteBtn];

Making series of buttons into an array ios

I have about 10-12 buttons that I'm adding to my scrollview. How can I make these into an array of buttons so that I can simplify the code? As of right now my code(only first three buttons are shown) is as follows:
UIButton *redButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
redButton.frame = CGRectMake(0, 0, 50, 30);
redButton.tag = 2;
[redButton setTitle:#"red" forState:UIControlStateNormal];
redButton.backgroundColor = [UIColor redColor];
[redButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[redButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:redButton];
UIButton *blueButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
blueButton.frame = CGRectMake(70, 0, 50, 30);
blueButton.tag = 3;
[blueButton setTitle:#"blue" forState:UIControlStateNormal];
blueButton.backgroundColor = [UIColor blueColor];
[blueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[blueButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:blueButton];
UIButton *greenButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
greenButton.frame = CGRectMake(140, 0, 50, 30);
greenButton.tag = 5 ;
[greenButton setTitle:#"green" forState:UIControlStateNormal];
greenButton.backgroundColor = [UIColor greenColor];
[greenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[greenButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:greenButton];
...
Can you see if this is possible
- (void)addButtonsToScrollView
{
NSArray *buttons = #[#{#"Tag":#2,#"Title":#"red",#"Color":[UIColor redColor]},
#{#"Tag":#3,#"Title":#"blue",#"Color":[UIColor blueColor]},
#{#"Tag":#5,#"Title":#"green",#"Color":[UIColor greenColor]}];
CGRect frame = CGRectMake(0.0f, 0.0f, 50.0f, 30.0f);
for (NSDictionary *dict in buttons)
{
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
button.tag = [dict[#"Tag"] integerValue];
[button setTitle:dict[#"Title"]
forState:UIControlStateNormal];
button.backgroundColor = dict[#"Color"];
[button setTitleColor:[UIColor blackColor]
forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
frame.origin.x+=frame.size.width+20.0f;
}
CGSize contentSize = self.scrollView.frame.size;
contentSize.width = frame.origin.x;
self.scrollView.contentSize = contentSize;
}
You can use:
[NSArray arrayWithObjects:redButton,greenButton,blueButton,nil];
But might be better off using a NSDictionary
[NSDictionary dictionaryWithObjectsAndKeys:
redButton, #"red",
blueButton, #"blue",
greenButton, #"green",
nil];
That way you can use the key to look them up instead of index.
Ok, we have the solution, try this code mate,
-(void) createButtons{
NSDictionary *buttonColors = #{#"Red":[UIColor redColor],#"Green":[UIColor greenColor],#"Black":[UIColor blackColor],#"Yellow":[UIColor yellowColor],#"Blue":[UIColor blueColor]};
int tag = 1;
for(NSString *key in buttonColors.allKeys){
UIColor *color = [buttonColors objectForKey:key];
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect frame = CGRectMake(((tag -1)*70), 0, 50, 30);
[button setFrame:frame];
button.tag = tag;
button.backgroundColor = color;
[button setTitleColor:color forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:key forState:UIControlStateNormal];
[self.scrollView addSubview:button];
tag++;
}
}

Resources