UIButton在自定义UIView中的麻烦

发布于 10-18 21:58 字数 1133 浏览 4 评论 0原文

我有一个简单的(基于视图的)应用程序。我想点击自定义 UIView,我的按钮移动到该视图内的某个位置(例如指向 10,10)。

  1. 我的自定义 UIView 是 DrawView(DrawView.h 和 DrawView.m)。
  2. RotatorViewController(h.和.m)。

我向我的 DrawView 添加一个 UIButton,连接我的 DrawView 和 UIButton 的插座。我在 RotatorViewController 和 @selector(tap:) 中添加 UITapGestureRecognizer。 的代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:drawView action:@selector(tap:)];
    [drawView addGestureRecognizer:tapGR];
    [tapGR release];

}

这是 UITapGestureRecognizer @selector(tap:)

- (void) tap:(UITapGestureRecognizer *)gesture {
        myButton.transform = CGAffineTransformMakeTranslation(10, 10);
}

,但是当我点击 DrawView 应用程序中的任何位置时都会崩溃。这是来自控制台的日志

2011-02-23 20:59:24.897 Rotator[7345:207] -[DrawView tap:]: unrecognized selector sent to instance 0x4d0fa80
2011-02-23 20:59:24.900 Rotator[7345:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DrawView tap:]: unrecognized selector sent to instance 0x4d0fa80'

我需要你的帮助

I have a simple (view-based) application. I want on tapping on custom UIView my button moved somewhere inside that view (for example to point 10,10).

  1. My custom UIView is DrawView (DrawView.h and DrawView.m).
  2. RotatorViewController (h. and .m).

I add to my DrawView an UIButton, connect with outlets my DrawView and UIButton. I add UITapGestureRecognizer in RotatorViewController and @selector(tap:).
Here is code of UITapGestureRecognizer

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:drawView action:@selector(tap:)];
    [drawView addGestureRecognizer:tapGR];
    [tapGR release];

}

@selector(tap:)

- (void) tap:(UITapGestureRecognizer *)gesture {
        myButton.transform = CGAffineTransformMakeTranslation(10, 10);
}

But when i tap anywhere in DrawView application crashes. Here is log from console

2011-02-23 20:59:24.897 Rotator[7345:207] -[DrawView tap:]: unrecognized selector sent to instance 0x4d0fa80
2011-02-23 20:59:24.900 Rotator[7345:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DrawView tap:]: unrecognized selector sent to instance 0x4d0fa80'

I need your help

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

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

发布评论

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

评论(2

丶视觉2024-10-25 21:58:04

你说:

UITapGestureRecognizer 在
旋转器视图控制器和
@selector(点击:)

你写道:
UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:drawView action:@selector(tap:)];

这意味着操作是在drawView委托中执行的,但您定义了选择器tap:< /code> 在 RotatorViewController 中。

我认为你只需要将目标 drawView 替换为 self

UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)] ;

You said :

UITapGestureRecognizer in
RotatorViewController and
@selector(tap:)

and you wrote :
UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:drawView action:@selector(tap:)];

It means the actions is performed in the drawView delegate but you defined the selector tap: in the RotatorViewController.

I think you just have to replace the target drawView by self

UIGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

屌丝范2024-10-25 21:58:04

该错误表明 DrawView 类的实例不响应 tap: 消息。我看到您在 DrawView.m 中定义了 tap: 方法,但是您是否在标头中声明了该方法?就像这样:

DrawView.h

@class UITapGestureRecognizer;
@interface DrawView {
    UIButton *myButton;
}
- (void) tap:(UITapGestureRecognizer *)gesture;
@end

The error indicates that instances of the DrawView class do not respond to tap: messages. I see that you have defined a tap: method in DrawView.m, but have you declared that method in the header? Like so:

DrawView.h

@class UITapGestureRecognizer;
@interface DrawView {
    UIButton *myButton;
}
- (void) tap:(UITapGestureRecognizer *)gesture;
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文