1

In my app I have 5 buttons. When firstButton.selected = YES; I need others to be *.selected = NO;. Is it a correct solution to write "NO" for other four buttons?

Here is a sample code if it's unclear what I mean:

- (IBAction)setColorRed:(id)sender {

            _redColor.selected = YES;
            _greenColor.selected = NO;
            _blueColor.selected = NO;
            _yeallowColor.selected = NO;
            _clearColor.selected = NO;
    }

2) second part of the question:

self.redColor setBounds:CGRectMake(0, 0, 45, 45)];

Could anyone please explain in human language why if I change those "0, 0" button does not change its position? Like when setFrame is send.

3) third part of the question:

Sender is button. How could [sender isSelected] work if it is a property of UITableViewCell? and what is the equivalent for "notSelected"?

Thanks a lot in advance..

αƞjiβ
  • 3,056
  • 14
  • 58
  • 95
Edgar
  • 898
  • 2
  • 12
  • 36

2 Answers2

1
  1. You should put your buttons in an Array - then you could simply track the selected index and have one button pressed method and eliminate duplicated code.

e.g.

- (IBAction)buttonTapped:(UButton *)sender {
    for (UIButton *aButton in self.buttons) {
        if (aButton != sender) {
            aButton.selected=NO;
        }
    }
    sender.selected=YES;
 }
  1. Bounds are the item's coordinates in its own coordinate space, frame is the items coordinate in its parent's coordinate space. It doesn't make sense to try and offset the origin of the item in its own space.
  2. the 'opposite' of isSelected is simply to check for a false/NO value of `isSelected' -

e.g.

if (self.redColor.isSelected) {
      //The button isn't selected
}

finally, you should get in the habit of using self.property rather than _property unless you specifically want to bypass the setter/getter.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • 1. Button array looks similar to: `NSArray *buttonArray = [[NSArray alloc] initWithObjects:button1,button2,button3,nil];` ? 3. if (self.redColor.isSelected) { //this code will be executed only when the button will be selected? } 3. I did not know that _property bypasses setter/getter, why then my programm works? for ex. self.imageView = _image; When it's good to bypass setter/getter? Could my pogramm work paster if I will change all _property to self.property? Thanks a lot! – Edgar Mar 08 '15 at 11:25
  • 1. The array code looks right. 2. Yes 3. You would bypass the setter/getter when the setter/getter executes code that you explicitly don't want to run - it won't be faster with self.property, but it is the correct way - especially where the property is inherited from a superclass and you may not have visibility with what the setter/getter does – Paulw11 Mar 08 '15 at 11:28
  • Thank you for the clarification. One last question: what (aButton != sender) does? What is the name of "! or !=", how to google it? I have used "!" only once: _button.hidden = !_button.hidden, I know what it does, but dont know how to explain it to myself correctly.. – Edgar Mar 08 '15 at 20:38
  • != is simply "not equal" - so in this case, if the button at the array index is not the `sender` button, deselect it – Paulw11 Mar 08 '15 at 21:15
  • _button.hidden = !_button.hidden - what in human language "!" does here? p.s. =! is "is equal"? – Edgar Mar 08 '15 at 22:47
  • 1
    `==` means is equal, `!=` means is not equal. `!` in C means "logical not" - so for a boolean will toggle between true/false. These are basic C operators. I suggest you read up on the C language which is the root of C#, C++ and Objective-C – Paulw11 Mar 08 '15 at 23:10
1
  1. That's fine, five buttons isn't too much too handle on your own. I'm not sure what you are trying to do though.

  2. The position doesn't change because you are setting the bounds of an object that already has a position. When you set the frame that is what will set the position, the bounds will be based off of the frame you created. Here is a good explanation

  3. Sender is whatever is being used to call that method, being a button or a cell or whatever you choose. There is no such thing is as "not selected" it would just be .selected = NO whereas "selected" is .selected = YES.

If you need more clarification let me know.

Community
  • 1
  • 1
AJ B
  • 375
  • 2
  • 9
  • selected is a property of UITableView [as stated in UITableView class reference](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableViewCell_Class/index.html#//apple_ref/occ/instp/UITableViewCell/selected) why then UIButton also reacts on "selected"? Please could you give me a link where is written about prefix "is"? – Edgar Mar 08 '15 at 21:32
  • @Edgar I just meant that something that is selected would be found by using ".selected" there is no "is" prefix. I should have made that more clear. As for the rest of your question, UIButton also has a selected property, so if you have your UIButtons and UITableViewCells with the same target, they both will call that same method. I can't give you a straight answer to why this is happening in your code without seeing it. – AJ B Mar 08 '15 at 21:37
  • I have meant that in [UIButton class reference](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIButton_Class/index.html) there is no property "selected". About "is" - I could write if (self.redColor.isSelected) or if (self.redColor.selected == YES) it means the same.. but where I can read about prefix ? I am newbie in objective-c and I think I missed that lesson about . I want to learnwith what properties I can use , so maybe you could point me a name of that part in objective-c? – Edgar Mar 08 '15 at 22:45
  • 1
    selected is a property of UIButton. When you use "somebutton.selected = YES" you are using dot syntax, and when you do "[somebutton isSelected]" you are doing the same thing using an accessor method. Dot syntax is more concise, but you can use either. You can read more [here](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html) – AJ B Mar 08 '15 at 23:39