当我将 UIImages 用作按钮时,如何在 UIImages 上设置操作?

发布于 2024-10-17 18:48:08 字数 685 浏览 1 评论 0原文

你好 我使用图像作为按钮,所以如何在按下时放置事件方法,我的编码如下...

UIImageView* view = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"front_pag.png"]];
    view.frame = CGRectMake(0, 0, 320, 460);
    [self.view addSubview:view];


    new_button = [[UIButton alloc] init];
    UIImage *img1 = [UIImage imageNamed:@"new.png"];
    [new_button setImage:img1 forState:UIControlStateNormal];
    [new_button setFrame:CGRectMake(85, 430, 0, 0)];
    [new_button addTarget:self action:@selector(newMethod) 
         forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:new_button];

我应该做什么,因为在这种情况下,我无法调用 newMethod,即使我在同一个类中有这个方法。

我应该怎么办 ?

Hello
I am using images as buttons so how can I put event methods as they pressed, my coding is as below ...

UIImageView* view = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"front_pag.png"]];
    view.frame = CGRectMake(0, 0, 320, 460);
    [self.view addSubview:view];


    new_button = [[UIButton alloc] init];
    UIImage *img1 = [UIImage imageNamed:@"new.png"];
    [new_button setImage:img1 forState:UIControlStateNormal];
    [new_button setFrame:CGRectMake(85, 430, 0, 0)];
    [new_button addTarget:self action:@selector(newMethod) 
         forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:new_button];

what should I do, because in this case, I can't call newMethod, even I have this method in same class.

what should I do ?

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

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

发布评论

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

评论(2

回梦 2024-10-24 18:48:08

您应该将 addTarget 调用更改为:

[new_button addTarget:self action:@selector(newMethod:) 
     forControlEvents:UIControlEventTouchUpInside];

注意 newMethod: 名称后面的分号

这是因为您的选择器的签名应如下所示:

- (void) newMethod:(id) sender

这样它将被调用。

祝你好运

You should change your addTarget call to this:

[new_button addTarget:self action:@selector(newMethod:) 
     forControlEvents:UIControlEventTouchUpInside];

Pay attention to semicolon after the newMethod: name

This is because the signature of your selector should be as following:

- (void) newMethod:(id) sender

This way it will be called.

Good luck

不必在意 2024-10-24 18:48:08

我想你可以尝试一下这个

uiimage *img1 = [UIImage imageNamed:@"someImage.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];
[button setImage:img1 forState:UIControlStateNormal]
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

I think you can try this out,

uiimage *img1 = [UIImage imageNamed:@"someImage.png"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchUpInside];
[button setImage:img1 forState:UIControlStateNormal]
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文