How can I disable multiple buttons? - ios

I have 2 buttons on my view and i want to disable the first button when i click on an other button and disable the second when I click again on the button.
I have tried with this code
if (button1.enable = NO) {
button2.enable = NO;
}
So I have in a NavigationBar a "+" button and 5 disable buttons in my view.
When I push the "+" button I want to enable the first button and when I push again that enable the second…
Thanks

if (button1.enabled == YES)
{
button1.enabled = NO;
button2.enabled = YES;
}
else (button2.enabled == YES)
{
button2.enabled = NO;
button1.enabled = YES;
}
Is that what your looking for? It would be an IBAction for the other button.

button1.enable = YES should be button1.enable == YES
a better readable form: [button1 isEnabled]

You're saying
if (button1.enabled = NO) {
when you probably mean
if (button1.enabled == NO) {
= is the assignment operator, and == is the boolean equality operator. What you're doing at the moment is assigning YES to button1.enable, which obviously enables button1. Then, because button.enable is true, control enters the if's clause and enables button2.
EDIT: To answer your new question ("When I push the "+" button I want to enable the first button and when I push again that enable the second..."), let's say that you initialise the button states somewhere. In your #interface add an instance variable
NSArray *buttons;
so your interface declaration looks something like
#interface YourViewController: UIViewController {
IBOutlet UIButton *button1;
IBOutlet UIButton *button2;
IBOutlet UIButton *button3;
IBOutlet UIButton *button4;
IBOutlet UIButton *button5;
NSArray *buttons;
}
and then initialise buttons like so:
-(void)viewDidLoad {
[super viewDidLoad];
buttons = [NSArray arrayWithObjects: button1, button2, button3, button4, button5, nil];
[buttons retain];
for (UIButton *each in buttons) {
each.enabled = NO;
}
-(void)viewDidUnload {
[buttons release];
[super viewDidUnload];
}
Let's say you hook up the + button's Touch Up Inside event handler to plusPressed:. Then you'd have
-(IBAction)plusPressed: (id) button {
for (UIButton *each in buttons) {
if (!each.enabled) {
each.enabled = YES;
break;
}
}
}
Each time plusPressed: is called, the next button in the array will be enabled. (I'm writing the above away from a compiler; there may be syntax errors.)
You could also make buttons a property. I didn't, because other classes have no business accessing buttons.

Related

Disable Button until all textfields have been Filled Xcode

I have looked everywhere for this answer but I haven't gotten a simple easy to follow answer to this question. I have 8 text fields that I need to fill out before I click and submit before moving onto the next page. I have hooked up each of the textfields to view controller.h file but I don't know how to disable the submit button easily step by step. Thank you so much for the help in advance.
I have tried this from a previous post but I could not get it to work..
Make an Outlet for every UITextField and create an IBAction in your .h:
IBOutlet UITextField *textField1;
IBOutlet UITextField *textField2;
IBOutlet UITextField *textField3;
IBOutlet UIButton *button
- (IBAction)editingChanged;
Connect all the outlets and connect the IBAction to every textfield with editingChanged:
- (IBAction)editingChanged {
if ([textfield1.text length] != 0 && [textfield2.text length] != 0 && [textfield3.text length] != 0) {
[button setEnabled:YES];
}
else {
[button setEnabled:NO];
}
}
Note that you can also use [textfield.text isEqualToString:#""] and put a ! in front of it (!means 'not' in objective-c) to recognize the empty textField, and say 'if the textField is empty do...'
- (void)viewDidLoad {
[super viewDidLoad];
[button setEnabled:NO];
}
try this,
1) You need to add textfiled delegate function to all your textfiled.
.h
#interface ViewController : UIViewController <UITextFieldDelegate>
.m
- (void)viewDidLoad
{
[super viewDidLoad];
//set submit button disable
submitbtn.enable=NO
textfiled1.delegate = self;
textfiled2.delegate = self;
textfiled3.delegate = self;
textfiled4.delegate = self;
textfiled5.delegate = self;
textfiled6.delegate = self;
textfiled7.delegate = self;
textfiled8.delegate = self;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//check your all texfield length if not equal to zero in below if(condition)
if(alltextfiled_length != 0)
{
submitbtn.enable=YES
}
else
{
submitbtn.enable=NO
}
}
I suggest you read the documentation on connecting outlets.
To handle changes in the text fields your view controller class could conform to the UITextFieldDelegate protocol, assign your view controller as the delegate of each text field, and implement - (BOOL)textFieldShouldReturn:(UITextField *)textField which will be called when tapping the Return button on each text field's keyboard.

Cleanest way to get associated UIView from UIButton press handler

In my app I have three UIButtons, each with an associated UIView. When one of the buttons is pressed, I want to:
Highlight the pressed button
Un-highlight the other buttons
Hide the UIViews associated with the other buttons
Un-hide the UIView associated with the pressed button
My solution (below) works and isn't horrible, but I can't help but think there's a cleaner, more efficient way. Any suggestions?
-(IBAction)buttonPressed:(id)sender {
NSArray *buttonArray = [NSArray arrayWithObjects:button1, button2, button3, nil];
NSDictionary* buttonViewDict = #{button1.titleLabel.text : view1,
button2.titleLabel.text : view2,
button3.titleLabel.text : view3};
for (UIButton* button in buttonArray) {
[button setHighlighted:[button isEqual:sender]];
[((UIView*)[buttonViewDict objectForKey:button.titleLabel.text]) setHidden:![button isEqual:sender]];
}
}
You can use the tag property to identify your buttons and views.
Set up the tag values in Interface Builder or in -viewDidLoad, then use the tag value to identify which button was pressed:
- (IBAction)buttonPressed:(UIButton*)sender {
for (UIButton* button in _buttons) {
button.highlighted = button.tag == sender.tag;
}
for (UIView* view in _views) {
view.hidden = view.tag != sender.tag;
}
}
For what it's worth, I like your way. I would consider using the buttons as the keys and simplifying it like so -
NSDictionary *buttonViewDict = #{button1 : view1,
button2 : view2,
button3 : view3};
[buttonViewDict enumerateKeysAndObjectsUsingBlock:^(UIButton *button, UIView *view, BOOL *stop) {
view.hidden = !button.highlighted = sender == button;
}];
You may also want to store the dictionary as a property.

Check Which Order Buttons Are Pressed Objective-C

I am developing an app that I need to check which order buttons are pressed in. I had 3 buttons and if they are pressed in the incorrect order I will have a UIAlertView. How can I check the order of presses?
Thanks
You could wire up an action to the buttons (like "Touch Up Inside"), and have it log which buttons are pressed, and maybe have a counter incrementing as well. Then when the counter gets to three, have it go through the list of button presses, and verify if they are the order you anticipate.
Below is an example of what I mean. For this example, you have to wire up all 3 buttons "Touch Up Inside" to that same IBAction. Of course you replace the NSLogs with your UIAlertView, but this shows the gist of what I said.
#interface comboSOTestViewController ()
#property (strong, nonatomic) NSMutableArray *buttonTitles;
#end
#implementation comboSOTestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.buttonTitles = [[NSMutableArray alloc]init];
}
- (IBAction)comboButtonPress:(UIButton *)sender
{
[self.buttonTitles addObject:sender.titleLabel.text];
if (self.buttonTitles.count > 2)
{
BOOL bad = NO;
NSArray *correctOrder = #[#"Second", #"Third", #"First"];
for (int i=0; i < 3; i++)
{
if (![self.buttonTitles[i] isEqualToString:correctOrder[i]])
{
bad = YES;
}
}
if (bad == YES)
{
NSLog(#"WRONG ORDER");
}
else
{
NSLog(#"CORRECT ORDER");
}
}
}

Issues with UINavigationItem changing properties at runtime

i have a Navigation Bar, wich contains a Navigation Item, which contains 2 Bar Buttons, these are created in the Storyboard, and i wanted to change 1 of the buttons at runtime, now this works:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UINavigationItem *thisNavBar = [self myNavigationItem];
thisNavBar.rightBarButtonItem = nil; // this works, it gets removed
UIBarButtonItem *insertBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(insertSkemaItem:)];
thisNavBar.rightBarButtonItem = insertBtn; // this also works, it sets the btn
}
Now, in my other method, which is called by another controller, it does not work
- (void)callChildChange {
...
// remove old btn
UINavigationItem *thisNavBar = [self skemaNavigationItem];
thisNavBar.rightBarButtonItem = nil; // does not work?
}
There is nothing wrong with the method, it runs just fine, but the nav btn item does not get removed ?
skemaNavigationItem is a Navigation item, declared in the .h file which links the navigation item i made via the storyboard.
Your UI items need to be added to your code (by ctrl-dragging) in the header file (.h) so they can be publicly accessed from other classes/view controllers.
Presuming you've done this, hiding a UI item is best done by using
relevantClass.yourViewObject.hidden = YES;
or if you really need to delete it for good,
[relevantClass.yourViewObject.view removeFromSuperView];
Edits
Options for changing target method:
Declare #property (nonatomic, assign) BOOL myButtonWasPressed; and:
- (IBAction) myButtonPressed
{
if (!self.myButtonWasPressed)
{
// This code will run the first time the button is pressed
self.myButton.text = #"New Button Text";
self.myButtonWasPressed = YES;
}
else
{
// This code will run after the first time your button is pressed
// You can even set your BOOL property back, and make it toggleable
}
}
or
- (IBAction) myButtonWasPressedFirstTime
{
// do what you need to when button is pressed then...
self.myButton.text = #"New Button Text";
[self.myButton removeTarget:self action:#selector(myButtonPressedFirstTime) forControlEvents:UIControlEventTouchUpInside];
[self.myButton addTarget:self action:#selector(myButtonPressedAgain) forControlEvents: UIControlEventTouchUpInside];
}
- (IBAction) myButtonWasPressedAgain
{
// this code will run the subsequent times your button is pressed
}

iOS: setting buttons pressed state

I am trying to find the best approach to doing this. I have 5 custom buttons on a view controller and I am trying to have the button stay highlighted if it is clicked. I know how to do this but I am trying to only allow 1 button to be highlighted at a time. So if a user clicks a button and highlights it, but clicks another, then the most recent button clicked will stay highlighted and the previous will unhighlight. What would be the best way to accomplish this?
You should keep a reference to all your buttons (for example, if you use IB, have links in your code like #property (nonatomic, strong) IBOutlet UIButton *button1; for all your buttons).
Then link all your buttons to the same method for a press on the button. I'll call it buttonPressed.
Impement it like this :
- (IBAction)buttonPressed:(id)sender {
UIButton *buttonPressed = (UIButton*)sender;
NSArray *buttons = [NSArray arrayWithObjects:_button1, _button2, _button3, nil];
bool buttonIsHighlighted = NO;
// Check if a button is already highlighted
for (UIButton *button in buttons) {
if (button.highlighted) {
buttonIsHighlighted = YES;
}
}
// If a button is highlighted, un-highlight all except the one pressed
// If no button is highlighted, just highlight the right one
if (buttonIsHighlighted) {
for (UIButton *button in buttons) {
if (buttonPressed == button) {
buttonIsHighlighted = YES;
} else {
button.highlighted = NO;
}
}
} else {
buttonPressed.highlighted = YES;
}
}
I can't test this code but I'm pretty sure it should work. Let me know if something's wrong.
Solution 1:
Put your buttons in an NSArray and when user clicks on a button check if another is highlighted. If YES, unhighlight it and highlight the one was pressed. If NO, highlight directly the one pressed.
Solution 2:
You can save the highlighted button in a global variable declared in #interface or in a #property. When users click the new one unhighlight the previous.

Resources