在方法中以编程方式移动控件会发出警告“未找到方法”;和“控制可能不会响应”

发布于 2024-11-10 17:14:45 字数 855 浏览 6 评论 0原文

我有一个 UISegmentedView,用于向用户提供他们可以在我的程序中执行的两个计算之一的选项。如果分段控件是一种方式,它应该显示 4 个文本框及其标签,如果其他只有 3 个(并且计算也会改变)。

这一切都有效,我的问题是移动控件(我想隐藏您不需要的控件,以及移动屏幕上的控件以使其看起来不错)

我想将其放在一种方法中,并且只需传递 UIControl 即可移动它。例如,在我的 .h 中

-(void) moveControlUp:(UIControl*)controlToMove;

,然后在我的 .m 中

- (void)moveControlUp:(UIControl*)controlToMove
{
    controlToMove.frame = CGRectMake(controlToMove.frame.origin.x, controlToMove.frame.origin.y - 39, controlToMove.frame.size.width, controlToMove.frame.size.height); 
}

,然后当我实际上想要移动控件时(labelPointsHeader 是 UILabel,textPointsName 是文本框):

[labelPointsHeader moveControlUp];
[textPointsName moveControlUp];

这不起作用,并且出现两个错误。 “UILabel 可能不会响应‘-moveControlUp’”和“未找到方法‘-moveControlUp’(返回类型默认为 id)。当我点击此按钮时,我崩溃了,指出“无法识别的选择器已发送到实例”。

I have a UISegmentedView that I use to present the user with an option on one of two calculations they can do in my program. If the segmented control is one way, it should show 4 text boxes and their labels, if the other only 3 (and the calculation would change as well).

This all works, my problem is with moving the controls (I want to hide the ones you won't need, as well as move the controls on the screen to make it look nice)

I would like to put this in one method, and just pass the UIControl to move it. For example, in my .h

-(void) moveControlUp:(UIControl*)controlToMove;

Then, in my .m

- (void)moveControlUp:(UIControl*)controlToMove
{
    controlToMove.frame = CGRectMake(controlToMove.frame.origin.x, controlToMove.frame.origin.y - 39, controlToMove.frame.size.width, controlToMove.frame.size.height); 
}

and then when I actually want to move the controls (labelPointsHeader is a UILabel, textPointsName is a text box):

[labelPointsHeader moveControlUp];
[textPointsName moveControlUp];

This doesn't work, and I get two errors. "UILabel may not respond to '-moveControlUp'" and "Method '-moveControlUp' not found (return type defaults to id). I crash when I hit this, stating that 'unrecognized selector sent to instance'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˉ厌 2024-11-17 17:14:45

您在哪个类中定义了这些方法? (似乎不是一个类别)

假设您在 SomeClass 中定义了它们,那么您需要执行以下操作:

[someInstanceOfSomeClass moveControlUp:labelPointsHeader];

现在,您在第一种情况下向 UILabel 发送消息,但 UILabel 没有此方法。该方法位于您定义它的任何类中。

In which class did you define these methods? (doesn't appear to be a category)

Let's say you defined them in SomeClass, then you need to do this:

[someInstanceOfSomeClass moveControlUp:labelPointsHeader];

Right now you are sending a message to a UILabel in the first case, but UILabel does not have this method. The method is in whichever class you defined it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文