Buttons have many states represented as enumeration values. What you have here are just two of them:
UIControlStateSelected|UIControlStateDisabled
The whole list in UIControl.h
:
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2, // flag usable by app (see below)
UIControlStateApplication = 0x00FF0000, // additional flags available for application use
UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
};
So, what does this mean? As you can see in the enumeration definition, the values set to the enumerations are single set bits for each value. So, here is the binary representation of the above:
1 << 0 = 0000 0001 b
1 << 1 = 0000 0010 b
.. and so on
As you can see, the <<
is a shift operator, and we are simply shifting the bits to the left, so that we can combine these enumerations together in a singe variable! what do I mean? Here:
0000 0011 b // This is essentially two flags set, on for the highlighted state one for the disabled
So, we usually use bit wise OR operator to combine these flags:
0000 0001 | 0000 0010 = 0000 0011
Finally, when combining these flags, and sending them to the method to set the title, you are telling the underlying implementation to change the title for those two options in a single method call.
...
As for your second question about the code that doesn't work, it would be useful to describe what "doesn't work" mean. Does it crash? What is the desired output your looking for?
Edit:
I just read the comments, and there seems a few more issues need to be covered by this answer:
In order to change the state of the button pragmatically, you should call something like:
[sender setEnabled:NO];
The code you have just tells the button to display that text AFTER it has changed to the disabled state.
Another issue regarding the ||
operator, that certainly is an OR operation, just not a bit-wise OR. This operator is used for boolean expressions instead of bit-wise operations.