Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

Failure to override some methods when UIView is the super class #1035

Closed
jimgoog opened this issue Nov 14, 2017 · 2 comments
Closed

Failure to override some methods when UIView is the super class #1035

jimgoog opened this issue Nov 14, 2017 · 2 comments

Comments

@jimgoog
Copy link

jimgoog commented Nov 14, 2017

Overriding most methods appears to work in KotlinNative, but for whatever reason, the layout methods on UIView appear to give a compile error when you try to override them.
Specifically, they give errors like 'layoutSubviews' overrides nothing.

@ExportObjCClass
class MyView : UIView {
    constructor(frame: CValue<CGRect>) : super(frame);

    override fun layoutSubviews()  // Compile error here: 'layoutSubviews' overrides nothing
    {
        println("this function was called to layout subviews!!!")
    }
}

Checking the objc.kt file from an older version of KotlinNative suggests that maybe the problem is that layoutSubviews is defined in the interop as an extension method rather than a method on the base class?

How / What is the best practice for overriding this method on UIView?

@jimgoog jimgoog changed the title Failure to override some methods when UIView is the parent/base class Failure to override some methods when UIView is the super class Nov 14, 2017
@SvyatoslavScherbina
Copy link
Collaborator

maybe the problem is that layoutSubviews is defined in the interop as an extension method rather than a method on the base class?

Yes, this seems to be the case.
layoutSubviews is declared as category method in Objective-C headers, and category methods are currently represented as Kotlin extension methods.

Extension methods can't be overridden in Kotlin. However, as a workaround, you can declare layoutSubviews Kotlin method without override keyword, and add @ObjCAction annotation to make it visible through Objective-C virtual dispatch:

class MyView : UIView {
   @ObjCAction fun layoutSubviews() {
        ...
    }
}

(Note that @ObjCAction is applicable to methods with very strict constraints on their signatures, namely methods that can be used as actions in target-action pattern. That's why adding @ObjCAction is just a workaround).

@olonho
Copy link
Contributor

olonho commented Feb 21, 2018

Is there anything left with this issue?

@olonho olonho closed this as completed Mar 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants