How to use iOS RespondsToSelector in Monotouch?

I am trying to understand the pattern of using the ReplsToSelector function in Monotouch. For example, the following translation does not work. (LayoutMargins is used to set cell indentation in iOS 8)

Goal C:

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

at Monotouch

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

I'm sure I just have a problem with the name "setLayoutMargins". I also tried LayoutMargins. Can someone help 1) correct this statement and 2) help me understand the naming convention / pattern?

Thanks!

+5
source share
1 answer

I'm sure I just have a problem with the name "setLayoutMargins"

The selector ends with : in ObjC and should be in C # too, that is:

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

Note: optional : means that an argument is required when invoking the selector. This is why set* has it until the getter does.

An alternative to validation for selectors is version verification.

+13
source

Source: https://habr.com/ru/post/1203161/


All Articles