我正在学习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#中,这等同于两个具有不同重载签名的方法名splitViewController吗?
这是令人困惑的,因为方法是非常描述性的。
举第一个例子:
splitViewController是方法的名称,vc和orientation是我们传递给它的参数。shouldHideViewController和inOrientation是在UISplitViewDelegate .h声明中声明的参数的名称。
我说的对吗?试着确认我的学习是正确的,并且我得到了这里的概念。
当人们提到第一个方法时,他们把它称为splitViewController:shouldHideViewController:inOrientation,这对我来说来自C#是很奇怪的,因为我们仅仅通过方法名称来引用一个方法,并且理解它有多个重载。另外,在Obj-C中,这些不同的“重载”处理完全不同的事情,这对我来说是一个简单的范例。
任何想法..。
发布于 2013-05-06 03:46:34
- (BOOL) splitViewController:(UISplitViewController *)svc
shouldHideViewController:(UIViewController *)vc
inOrientation:(UIInterfaceOrientation)orientation方法名称:splitViewController:shouldHideViewController:inOrientation:。
参数名称:svc、vc、orientation。
Objective-C没有方法重载。您的代码显示了两种不同的方法。
在Obj-C中这些不同的“重载”处理完全不同的事情,这对我来说是一个简单的范例。
这里的范例是delegation,它是一种通过依赖于另一个类来扩展类行为的方法。
考虑一下这个虚构的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这是一个管理表对象的控制器。它声明自己符合协议,并将自己设置为表的委托。现在,您可以添加您认为合适的任何逻辑来返回给定行的高度(在本例中,它返回一个固定值)。
我们不需要子类,我们只能实现我们感兴趣的委托方法。
https://stackoverflow.com/questions/16388119
复制相似问题