xCode4 - 当事件更改时如何更改 UIbutton?

发布于 2024-12-21 15:06:52 字数 340 浏览 4 评论 0原文

我有一个应用程序,当按下按钮时,会发生一个操作..我可以使用以下方法在操作开​​始时更改按钮:

UIImage *changeImage = [UIImage imageNamed:@"stopGrey.png"];
[myButton setImage:changeImage forState:UIControlStateNormal];

操作结束后,我希望能够将按钮更改回来..我不能弄清楚如何做到这一点..

我已经尝试过 UIControlStateDisabled/Selected/Application..我已登录以确保收到操作结束。

感谢您的帮助..

I have an app, where when the button is pressed, an action happens .. I can get the button to change when the action starts by using:

UIImage *changeImage = [UIImage imageNamed:@"stopGrey.png"];
[myButton setImage:changeImage forState:UIControlStateNormal];

After the action ends, I want to be able to change the button back.. I cannot figure out how to do this..

I've tried UIControlStateDisabled/Selected/Application.. I've logged to make sure the end of action is being received.

Thanks for any help..

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

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

发布评论

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

评论(2

唐婉 2024-12-28 15:06:53

只需将类似这样的内容放入 viewDidLoad 中即可:

[myButton setImage:[UIImage imageNamed:@"Normal.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"Selected.png"] forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:@"Highlighted.png"] forState:UIControlStateHighlighted];
[myButton setImage:[UIImage imageNamed:@"Disabled.png"] forState:UIControlStateDisabled];

然后按钮本身将处理图像更改。
如果您希望在完成后台任务时明显禁用按钮,只需将启用设置为“否”,它将显示禁用的图像。

您可以使用 touchUpInside 目标来测试这些设置,例如:

-(void)buttonPushed:(id)sender{
    [myButton setEnabled:NO];

    // Simulate pause before stuff is done
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [myButton setEnabled:YES];
    });
}

Simply put something like this in viewDidLoad:

[myButton setImage:[UIImage imageNamed:@"Normal.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"Selected.png"] forState:UIControlStateSelected];
[myButton setImage:[UIImage imageNamed:@"Highlighted.png"] forState:UIControlStateHighlighted];
[myButton setImage:[UIImage imageNamed:@"Disabled.png"] forState:UIControlStateDisabled];

Then the button itself will handle the image changes.
If you want the button to be visibly disabled while completing a background task simply set enabled to NO and it will show the disabled image.

You can test these setting by using an touchUpInside target like:

-(void)buttonPushed:(id)sender{
    [myButton setEnabled:NO];

    // Simulate pause before stuff is done
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [myButton setEnabled:YES];
    });
}
高速公鹿 2024-12-28 15:06:52

好的,您需要这样做:

.h 文件:

-(IBAction)ButtonPressed:(id)sender;
-(IBAction)ButtonReleased:(id)sender;

如果您使用图形部分,请将 touch down 事件加入到方法 ButtonPressed 中,并将 touch up 事件加入到 ButtonReleased 中(右键单击按钮以显示这些选项)。如果您使用代码添加按钮,请使用以下方法(在 .m 文件的 viewdidload 方法中):

[button addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(ButtonReleased:) forControlEvents:UIControlEventTouchUpInside];

然后就完成了。每次触摸按钮时都会调用 ButtonPressed 方法,每次松开其中的按钮时都会调用 ButtonReleased 方法。对于您的问题,您可以将以下代码添加到方法(.m 文件)中,

-(IBAction)ButtonPressed:(id)sender
{
    [button setBackgroundImage:[UIImage imageNamed:@"ImageWhenPressed.png"] forState:UIControlStateNormal];
}
-(IBAction)ButtonReleased:(id)sender
{
    [button setBackgroundImage:[UIImage imageNamed:@"ImageWhenReleased.png"] forState:UIControlStateNormal];
}

我还猜测您不知道如何将图形文件中绘制的按钮链接到代码中的指针。首先,在 .h 文件中创建一个指针:

IBOutlet UIButton *button;

然后,转到图形文件并右键单击并将文件所有者图像(左侧有一个橙色透明立方体作为图像)拖动到按钮。然后,选择具有指针名称的选项。在本例中,按钮。

在那里!你完成了!

Ok, you need to do this:

.h file:

-(IBAction)ButtonPressed:(id)sender;
-(IBAction)ButtonReleased:(id)sender;

if you are using the graphical part join the touch down event to the method ButtonPressed and the touch up inside to ButtonReleased (right click the button for these options to appear). If you are using code to add the buttons use the method (in the viewdidload method of the .m file):

[button addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(ButtonReleased:) forControlEvents:UIControlEventTouchUpInside];

Then you're done. The method ButtonPressed will be called every time you touch the button and the ButtonReleased method will be called every time the you let go of the button inside of it. For your question, you can add the following code to the methods (.m file)

-(IBAction)ButtonPressed:(id)sender
{
    [button setBackgroundImage:[UIImage imageNamed:@"ImageWhenPressed.png"] forState:UIControlStateNormal];
}
-(IBAction)ButtonReleased:(id)sender
{
    [button setBackgroundImage:[UIImage imageNamed:@"ImageWhenReleased.png"] forState:UIControlStateNormal];
}

I am also guessing that you don't know how to link a button drawn in the graphical file to a pointer in the code. First you create a pointer in the .h file:

IBOutlet UIButton *button;

Then, you go to the graphical file and right click-drag the file owner image (on the left, has a orange-transparent cube as an image) to the button. Then, you select the option that has the name of the pointer. In this case, button.

And there! you're done!

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