Post

Replies

Boosts

Views

Activity

Reply to UIButton extension method not called
I think I just ran into this issue in native objective-c code. I am not using swift. I defined a category on UIButton and created a setTitle method: @interface UIButton (utility) (void) setTitle:(NSString*)title; @end And my code never gets called. I finally changed the name to "setText". I really want to know why this is breaking. It makes me worry that using categories is just plain dangerous. Especially if an upgrade of the OS version could potentially break an app that is already distributed because of this type of problem. After change the name to "setText" I added some debugging code to probe for a selector named "setTitle:" and sure enough, there was already a method, presumably defined by Apple. Here was my code:   if ([button respondsToSelector:@selector(setTitle:)])  {   NSLog(@"xyz: has selector setTitle:");  }  else  {   NSLog(@"xyz: DOES NOT have setTitle:");  } And it did log "xyz: has selector setTitle:". I am thinking that either Apple created a new PRIVATE method "setTitle:" or their own extension to UIButton (or sub-class) named "setTitle:". I lean towards the extension since I believe that a naming conflict with extensions causes ambiguous behavior. But I would like to know what it going on. In the end, I just wish they would add the official method to UIButton for "setTitle" without the state parameter so we would not have to "patch things up".
Topic: UI Frameworks SubTopic: UIKit Tags:
Jan ’21