-2

I have a UIButton object in my program. I want to use it like follows

myButton.setImage:blablabla
mybutton.title:.......blabla
...
...
myButton.placeTextBelowImageWithSpacing:12

While calling my method "placeTextBelowImageWithSpacing:12" it must set the image and text accordingly. I have the method ready with me. How can i use it in the above way.

PS: I hate subclassing. Thanks in Advance

VARUN ISAC
  • 443
  • 3
  • 13

3 Answers3

1

Create a custom subclass of UIButton. I created a button called FinderButton that has an image and a title centered below it. It works great.

If you hate subclassing then you might want to think about a different line of work.

Being an Objective C programmer that hates subclassing is a bit like being a surgeon who hates blood or a farmer who hates dirt. Defining a class hierarchy is one of the main tools for doing development in an OO language like Objective-C.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • **Being an Objective C programmer that hates subclassing is a bit like being a surgeon who hates blood** - Wow – Kumar KL Jun 13 '14 at 12:34
1

You can do this by creating a UIButton category:

UIButton+MyCustomMethod.h

@interface UIButton (MyCustomMethod)

- (void)placeTextBelowImageWithSpacing;

@end

UIButton+MyCustomMethod.m

@implementation UIButton (MyCustomMethod)

- (void)placeTextBelowImageWithSpacing
{
    // ...
}

@end
Vame
  • 2,033
  • 2
  • 18
  • 29
0

You can't. That isn't valid syntax in Objective-C. The closest you can get to that would be to explicitly declare new properties on UIButton that followed your naming convention. Using them would then look like:

myButton.setTitle = @"something"

Then you could override setTitle's setter (setSetTitle:), and making it call setTitle:forControlState:, which I'm assuming is your goal.

But this should only be done through subclassing (learn to love it, it's a big part of OOP), although if you really really want to, you can add the properties in a category using the Objective-C runtime objc_setAssociatedObject() function more info here: Objective-C: Property / instance variable in category

Community
  • 1
  • 1
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281