2014-09-22 72 views
5

我想了解在Monotouch中使用ResponsdsToSelector的模式。例如,以下翻译不起作用。 (LayoutMargins用来在iOS中设置单元格缩进8)如何在Monotouch中使用iOS RespondsToSelector?

目标C:

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

到MonoTouch的

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

我敢肯定,我只是有一个问题,我命名“ setLayoutMargins”。我也尝试过“LayoutMargins”。任何人都可以帮助1)修正这个声明和2)帮助我理解命名约定/模式?

谢谢!

回答

13

我敢肯定,我只是用我的命名问题“setLayoutMargins”

的选择与:在ObjC结束,需要有在C#太,即:

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

注:额外:意味着有调用选择时所需的参数。这就是为什么set*有它,而吸气没有。

来检查选择另一种方法是使用一个版本检查。

+0

太棒了,难怪我无法得到它。从未想过要添加“:”。感谢您的快速回答! – Ender2050 2014-09-22 18:55:38