我正在尝试编写一个通用方法,允许我传递以下内容:“x”,“y”“object”然后让它移动。目前我有这个:
-(void) changeObjectLocations: (integer_t) xSpot: (integer_t) ySpot: (id) sender {
if (![sender isKindOfClass:[UIButton class]])
{
UIButton *myObject = (UIButton *)sender;
[UIView animateWithDuration:1.5
animations:^{
CGRect newFrame = myObject.frame;
newFrame.origin.x = xSpot;
newFrame.origin.y = ySpot;
myObject.frame = newFrame;
}];
}
else if (![sender isKindOfClass:[UILabel class]])
{
UILabel *myObject = (UILabel *)sender;
[UIView animateWithDuration:1.5 animations:^{
CGRect newFrame = myObject.frame;
newFrame.origin.x = xSpot;
newFrame.origin.y = ySpot;
myObject.frame = newFrame;
}];
}
}
然后我想这样称呼它:
-(void) orientationBlockLandscape {
[self changeObjectLocations: 456 :282 : btn1] ;
[self changeObjectLocations: 391 :227 : lblTitle] ;
}
虽然它正在工作,但在编译时我得到以下警告:
SecondViewController.m:33:警告:'SecondViewController'可能无法响应'-changeObjectLocations :::'
有没有更好的方法可以/应该传递对象?提前感谢您提供的任何帮助。
...地理位置
答案 0 :(得分:1)
根据输出的警告,听起来好像你没有在SecondViewController的标题中定义changeObjectLocations
- 或者它与你实现的标签不同。
答案 1 :(得分:0)
编译器查找选择器,这些选择器是带有变量的方法定义并且返回已删除。
这样的方法如下:
-(void) setObject:(id) anObject forKey:(NSString *) keyname;
...将有一个选择器:
setObject:forKey:
因此,您的方法:
-(void) changeObjectLocations: (integer_t) xSpot: (integer_t) ySpot: (id) sender
...将有一个选择器:
changeObjectLocations:xSpot:ySpot:
请注意,参数名称是选择器的一部分,所以:
changeObjectLocations:xSpot:ySpot:
和
changeObjectLocations:::
..是两个完全独立的选择器,代表两个完全独立的方法。
尽管使用没有名称的参数在语言中是合法的,例如“:::”这种做法非常非常糟糕,主要是因为很容易发生命名冲突。显式不仅使代码更易于阅读和维护,而且使编译器和运行时更容易运行。