Avoiding compiler warnings when doing message forwarding

后端 未结 2 827
既然无缘
既然无缘 2021-01-19 11:54

I created a class that wraps a UITextView and adds some ui elements. I want the new class\' API to be identical with UITextView, so I use message forwarding (listing below)

2条回答
  •  有刺的猬
    2021-01-19 12:51

    Declare a category that provides the method declarations of the methods you are forwarding:

    @interface MyTextField (ImGonnaLetYouFinishButFirstImForwardingThese)
    ... methods you want to forward here ...
    @end
    

    No need to provide an @implementation.

    Note that this is a fairly atypical pattern. Not fairly, very. There should be no reason why you can't subclass. You say Subclassing UITextView is also out of the question, because I need to insert views below the text view, but that isn't true.


    If I subclass UITextField, all I can do with the other views is to add them as subviews, which means they will be on top of the text field. I guess I could modify drawRect:... Is that what you would suggest? Or what do you have up your sleeve there?

    If you need a group, create a UIView subclass that manages the various subviews appropriately, no forwarding necessary. Then you can order the views however you like.

    Forwarding is used extremely rarely. Down that path lies madness. It really sounds like your design is a bit in the weeds, but there isn't enough information to really say anything more specific.

提交回复
热议问题