nextResponder in Swift 3

2019-04-16 07:55发布

in Swift 2 this code works fine

override func nextResponder() -> UIResponder? {
  // it's called when user make any touch event
  return super.nextResponder()
}

it's basically helps to get notification about any user interaction.

in Swift 3 now it's a variable not a function and I can't even observe it with didSet.

so how to use the code above in Swift 3

2条回答
Bombasti
2楼-- · 2019-04-16 08:24

nextResponder is replaced by next in Swift 3. Its new declaration is:

var next: UIResponder? { get }
查看更多
在下西门庆
3楼-- · 2019-04-16 08:27

You can override the variable:

override var next: UIResponder? {
    get {
        // do something
        return super.next
    }
}

As the variable is defined as a read-only variable, implementing didSet doesn't make sense. You're not supposed to set this variable. Instead, different subclasses of UIResponder can override the variable and return different values. The base class returns nil.

Note: The variable is called next in Swift 3 and nextResponder in Objective-C and earlier Swift versions. This can be seen in the documentation when you switch between Swift and Objective-C in the right column.

查看更多
登录 后发表回答