Ender2050
Ender2050

Reputation: 6992

How to use iOS RespondsToSelector in Monotouch?

I'm trying to understand the pattern for using ResponsdsToSelector in Monotouch. For example, the following translation doesn't work. (LayoutMargins is used to set the cell indent in iOS 8)

Objective C:

if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    [tableView setLayoutMargins:UIEdgeInsetsZero];
}

to Monotouch

if (this.TableView.RespondsToSelector(new Selector("setLayoutMargins")))
    this.TableView.LayoutMargins = UIEdgeInsets.Zero;

I'm pretty sure I just have a problem with my naming "setLayoutMargins". I've tried "LayoutMargins" too. Can anyone help 1) fix this statement and 2) help me understand the naming convention / pattern?

Thanks!

Upvotes: 5

Views: 2696

Answers (1)

poupou
poupou

Reputation: 43553

I'm pretty sure I just have a problem with my naming "setLayoutMargins"

The selector ends with a : in ObjC and needs to have in in C# too, i.e.:

if (this.TableView.RespondsToSelector(new Selector("setLayoutMargins:")))

Note: that extra : means there's an argument required when calling the selector. That's why the set* has it while the getter does not.

An alternative to checking for selectors is to use a version check.

Upvotes: 15

Related Questions