【问题标题】:Delegate Method Signatures in Obj-C, Understanding what their C# equivalents would beObj-C 中的委托方法签名,了解它们的 C# 等价物是什么
【发布时间】:2013-04-29 13:56:55
【问题描述】:

我正在学习 Objective C。我正在尝试在 C# 中找到方法签名的等价物。

我对 UIViewControllerDelegate 的以下签名感到困惑

- (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc

那么,在 C# 中,这将等同于具有不同重载签名的 2 个方法名称 splitViewController?

这很令人困惑,因为这些方法非常具有描述性......

举第一个例子:

splitViewController 是方法的名称,vc 和orientation 是我们传递给它的参数。 shouldHideViewController 和 inOrientation 是在 UISplitViewDelegate .h 声明中声明的参数的名称。

我说得对吗?试图确认我的学习正确并且我在这里得到了概念。

当人们提到他的第一种方法时,他们将其称为 splitViewController:shouldHideViewController:inOrientation 这对来自 C# 的我来说很奇怪,因为我们只会通过方法名称来引用一个方法并理解它有多个重载。另外,在 Obj-C 中,这些不同的“重载”确实可以完全处理不同的事情,这对我来说是一个战略范式。

任何想法...

【问题讨论】:

  • afaik objc 中的“签名”是整个“选择器”(splitViewController:shouldHideViewController:inOrientation),这是识别被调用代码的唯一方法:在其他意义上没有重载langs,所以你需要明确整个选择器来识别将被调用的“方法”。

标签: ios objective-c


【解决方案1】:
- (BOOL) splitViewController:(UISplitViewController *)svc 
    shouldHideViewController:(UIViewController *)vc 
               inOrientation:(UIInterfaceOrientation)orientation

方法名称:splitViewController:shouldHideViewController:inOrientation:.
参数名称:svcvcorientation

Objective-C 没有方法重载。您的代码显示了两种不同的方法。

在 Obj-C 中,这些不同的“重载”确实处理不同的事情 完全,这对我来说是一个战略范式。

这里的范例是委托,这是一种通过依赖另一个类来扩展一个类的行为的方法。

考虑一下这个虚构的 API:

@interface TableDelegate
-(CGFloat)heightForRow:(NSUInteger)row;
@end

@interface Table
@property (weak) id<TableDelegate> delegate;
@end

这是一个具有委托属性的表对象。建表时,它会询问代理每行的高度应该是多少。

@interface Controller <TableDelegate>{
    Table _table;
}
@end

@implementation Controller
-(instancetype)init {
    if (self=[super init]){
        _table = [Table new];
        _table.delegate = self;
    }
    return self;
}
-(CGFloat)heightForRow:(NSUInteger)row {
  return 10.f;
}
@end

那是一个管理表对象的控制器。它声明自己符合协议并将自己设置为表的委托。现在您可以添加任何您认为合适的逻辑来返回给定行的高度(在示例中它返回一个固定值)。

我们不必子类化,我们可以只实现我们感兴趣的委托方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2013-10-15
    • 1970-01-01
    相关资源
    最近更新 更多