简体   繁体   English

iPhone - 如何刷新/重绘已更改状态的按钮?

[英]iPhone - how do I refresh/redraw a button that has changed state?

I am currently using NSNotifications to send a message from my app delegate to a UIView in my iPhone application. 我目前正在使用NSNotifications从我的应用代表向我的iPhone应用程序中的UIView发送消息。

The notification gets received by the UIView and I then use a selector to change the state of a button on the UIView. UIView收到通知,然后我使用选择器来更改UIView上按钮的状态。

Here is the function that is passed to the selector: 这是传递给选择器的函数:

-(void)changeButtonState
{
    NSLog(@"Change button state received");
    [testButton setEnabled:NO];
    [testButton setNeedsDisplay];
}

This gets called however the button state never gets visually changed, it functionally becomes disabled but looks like its still enabled on the screen. 这会被调用,但按钮状态永远不会在视觉上被更改,它在功能上会被禁用但看起来仍然在屏幕上启用。

So it looks like setNeedsDisplay either doesn't work here or is incorrect coding on my part? 所以看起来setNeedsDisplay在这里不起作用或者我的编码不正确?

Can anyone tell me how I should properly redraw the button or view when I change the state? 任何人都可以告诉我当我改变状态时应该如何正确地重绘按钮或视图?

Since UI refreshing is usually done on the main thread, setNeedsDisplay will only get called on the main thread. 由于UI刷新通常在主线程上完成,因此只能在主线程上调用setNeedsDisplay Therefore, you need to make sure you are calling it on the main thread by using performSelectorOnMainThread . 因此,您需要确保使用performSelectorOnMainThread在主线程上调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM