15

I went through the apple doc too but it just states that its

Additional control-state flags available for application use.

Its just a getter method so when does it get set?

luk2302
  • 55,258
  • 23
  • 97
  • 137
Apogee
  • 689
  • 8
  • 19
  • I think your question body+title is a bit misleading since the `UIControlState.application` is not at all specific to `UIButton`, and it is not a method either. Are you asking what the use of `UIControlState.application` is and at what time the `state` property of any UI-element assumes the `.application` value? – luk2302 May 03 '17 at 12:09
  • see this :https://developer.apple.com/reference/uikit/uicontrolstate – KKRocks May 03 '17 at 12:11
  • @KKRocks I think OP saw that already since the sentence quoted is exactly the same. – luk2302 May 03 '17 at 12:12

1 Answers1

11

application and reserved are basically markers. That is more clear when looking at the objective-c documentation for them:

disabled: UIControlStateDisabled = 1 << 1

application: UIControlStateApplication = 0x00FF0000

reserved: UIControlStateReserved = 0xFF000000

That means that the second least significant bit of a UIControlState for example is responsible for determining wether or not a UIControl is disabled or not. All the bits from 17 - 24 (from 1 << 16 until 1 << 23) are there for your application to use while 25 - 32 (from 1 << 24 until 1 << 31) are there for internal frameworks to use.

That basically means that Apple is able / allowed to define new state flags of controls while using the lowest 16 bits, you have the guarantee to be able to use 8 bits for custom flags of your own.

Defining custom flags can be done e.g. via:

let myFlag = UIControlState(rawValue: 1 << 18)

class MyButton : UIButton {
    var customFlags = myFlag
    override var state: UIControlState {
        get {
            return [super.state, customFlags]
        }
    }

    func disableCustom() {
        customFlags.remove(myFlag)
    }
}

which can be used via

let myButton = MyButton()
print(myButton.state.rawValue) // 262144 (= 2^18)
myButton.isEnabled = false
myButton.isSelected = true
print(myButton.state.rawValue) // 262150 (= 262144 + 4 + 2)
myButton.disableCustom()
print(myButton.state.rawValue) // 6 (= 4 + 2) 
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • Your override var looks like it returns an array of UIControlState yet it returns a UIControlState with the state and customFlags XOR'ed together. How is this? – Brynjar Feb 13 '18 at 09:14
  • 1
    @Brynjar https://oleb.net/blog/2016/09/swift-option-sets/ - they conform to [`ExpressibleByArrayLiteral`](https://developer.apple.com/documentation/swift/expressiblebyarrayliteral) – luk2302 Feb 13 '18 at 10:28
  • @Brynjar and it does not work like `XOR`, it is a bitwise `OR` or simply a `+` since no single bit is `1` in two different flags. – luk2302 Feb 13 '18 at 11:16
  • Thx. Ole's blog is always good. Ok sure, a bitwise OR, that said XOR would return the same result ; ) – Brynjar Feb 13 '18 at 15:00