views:

161

answers:

3

hi, 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

A: 

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.

Frank Shearar
okok thanks i try
Alex
thanks for your help!but I don't understand where I have to put that code...
Alex
thank you but when I add the code i have a error "Expected expression before 'UIButton' for the line "foreach (UIButton *each in Buttons)thanks again
Alex
Try again? I had a ; where I should have written a {
Frank Shearar
no, always the same error...
Alex
and i have a 1 warning :implicit declaration of function 'foreach'
Alex
Like I said: I'm away from a compiler. Turns out that a foreach loop uses "for", not "foreach". Please try again.
Frank Shearar
no..., now when i click on the "+" the app crash... and i don't have any warning or error...
Alex
i really don't know why...
Alex
Have you tried putting a breakpoint on the for and seeing what the various bits look like? Are button1..button5 actually hooked up to the bits in the nibfile?
Frank Shearar
yes, i have tried and I have a syntax error :/ and yes my buttons are connected :/
Alex
I'm missing something. You say your application crashes, which indicates that it compiles, which means the syntax is fine... but you have a syntax error? What is the error? Are button1..button5 initialised (non-nil) in viewDidLoad?
Frank Shearar
Oops sry, I actually don't speak English i normally speak French... And for the Buttons in my viewDidLoad I have this Button1.enabled = NO; Button2.enabled = NO; Button3.enabled = NO; Button4.enabled = NO; Button5.enabled = NO;Thanks
Alex
yes so when I push the "+" button the app crash
Alex
the console say that "terminate called after throwing an instance of 'NSException'reason: '-[NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x6b46610'"
Alex
I have try in a new project with only your code and it's doesn't work... The five buttons are not enabled but when i click nothing happensI don't have any warning or error.Thanks for your help
Alex
So I assume that the viewDidLoad part above works correctly, right? When you put a breakpoint in plusPressed and click the + control does actually reach that breakpoint? What type is each in the for loop? (You mention "Button1.enabled = NO" - Button1 is not the same as button1.)
Frank Shearar
hi, when I put a breakpoint in plusPressed and press the + my app crash and Xcode show this line "for (UIButton *each in Buttons) {"and I have renamed buttons,button1,2,3,4,5 for Buttons,Button1,2,3,4,5.Thanks
Alex
The algorithm's sound. The problem's that buttons needs to be retained.
Frank Shearar
A: 

button1.enable = YES should be button1.enable == YES

a better readable form: [button1 isEnabled]

vikingosegundo
+2  A: 
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.

thyrgle