-1

I was woundering if it is possible to make an action that can receive different types of objects. For example if I want to pass UILabels and UIButtons, to the same action, but only one at the time. What I'm not looking for is something like this:

- (void)actionInitWithButton:(UIButton *)button Label:(UILabel *)label;

More something like this:

- (void)actionInitWithObject:(UniversalObject *)object;

Thanks in advance :)

Eksperiment626
  • 985
  • 3
  • 16
  • 30

1 Answers1

1

In Objective-C, they're simply called id. It's a principal concept of the language.

The alternative is NSObject, but this is typically used for subclassing -- id covers a more broad spectrum as not all (but most) objects inherit from NSObject.

Ex.

- (void)actionInitWithObject:(id)something
{
    if([something isKindOfClass:[UIButton class])
    {
        UIButton *button = (UIButton *)something;
        ...
    } else if ([something isKindOfClass:[UILabel class]) {
        UILabel *label = (UILabel *)something;
        ...
    }
 }
John
  • 2,675
  • 2
  • 18
  • 19
  • Thank you very much :)... my next problem is now that I can't find center on an id object.. would you know what to do here – Eksperiment626 Dec 13 '12 at 04:02
  • 1
    If you're going to treat the `id` as anything but a generic pointer, you need to cast it. XCode will let you cast an `id` to any object, so you need to protect against runtime exceptions. – John Dec 13 '12 at 04:05