1

Hello everyone i have a problem with save tag of button when i click:

- (IBAction)addButtonSeconds:(id)sender {


if ([sender tag] == 1) {
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    [standardUserDefaults setObject:@"Off" forKey:@"time"];
    [_editButton setImage:[UIImage imageNamed:@"ВЫКЛ.png"] forState:UIControlStateNormal];
    [_plus setEnabled:YES];
    [_minus setEnabled:YES];
    [_editButton setTag:2];
} else if ([sender tag] == 2){
    [_editButton setImage:[UIImage imageNamed:@"ВКЛ.png"] forState:UIControlStateNormal];
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    [standardUserDefaults setObject:@"ON" forKey:@"time"];
    [_plus setEnabled:NO];
    [_minus setEnabled:NO];
    [_editButton setTag:1];
}

This is initialise the method

 if ([[standartDefaults stringForKey:@"time"]isEqualToString:@"ON"]) {
    self.editButton.tag = 2;
}else{
     self.editButton.tag = 1;
}

If someone could say me where is my problem.Thank you.

Tcacenco Daniel
  • 445
  • 7
  • 17
  • 1
    What's the problem? What's wrong with the code you posted? – rmaddy Oct 30 '14 at 18:54
  • 1
    I would question your use of tags. – Abizern Oct 30 '14 at 18:54
  • 1
    I would echo that by saying I avoid the use of tags at all costs. They almost always promote holding state in the view which is something that you shouldn't really be doing. – Fogmeister Oct 30 '14 at 18:57
  • My question is when i click button and i go at other controller and after go back in this controller button must to save in those position what i was click for example in position of tag 2. – Tcacenco Daniel Oct 30 '14 at 19:14

1 Answers1

2

It looks like you are expecting different behaviour within your addButtonSeconds: than within your initialization.

At initialization:

self.editButton.tag == 2; // when "time" is "ON"
self.editButton.tag == 1; // when "time" is not "ON"

In addButtonSeconds::

self.editButton.tag == 1; // when "time" is "ON"
self.editButton.tag == 2; // when "time" is not "ON"

Still, I'm not sure that what you've written logically makes sense or necessitates the use of the tag property. Consider subclassing UIButton instead .. or simply use the existing setSelected: and isSelected of UIButton.

Edit: I'll just fix your code for you based on your comments.

- (IBAction)addButtonSeconds:(id)sender {
  [_editButton setSelected:![_editButton isSelected]];
  [_plus setEnabled:![_editButton isSelected]];
  [_minus setEnabled:![_editButton isSelected]];
  NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
  [standardUserDefaults setObject:@([_editButton isSelected]) forKey:@"editSelected"];
}

In your initialization:

[_editButton setImage:[UIImage imageNamed:@"ВЫКЛ.png"] forState:UIControlStateNormal];
[_editButton setImage:[UIImage imageNamed:@"ВКЛ.png"] forState:UIControlStateSelected];
[_editButton setImage:[UIImage imageNamed:@"ВКЛ.png"] forState:UIControlStateSelected|UIControlStateHighlighted];
[_editButton setSelected:[[standartDefaults stringForKey:@"editSelected"] boolValue]];
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • Thank you for answer i was change with setSelected but i want to save position of image when i click for example when i go at other controller and after go back my position of button must to be saved. – Tcacenco Daniel Oct 30 '14 at 19:40
  • I'm not sure I understand what you're asking. Your comment's question is completely different from your original question. Do you mean that you want to save the `frame.origin` of your `UIButton`? – Ian MacDonald Oct 30 '14 at 19:43
  • Yes for example when i click button image of button is change and after click agin he change with first image and i need to save image of button when is changing with NSUserDefaults. – Tcacenco Daniel Oct 30 '14 at 19:48