Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 393
Ender2050 Avatar asked Sep 22 '14 18:09

Ender2050


1 Answers

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.

like image 95
poupou Avatar answered Nov 07 '22 13:11

poupou