开发者

How can I disable multiple buttons?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜