我可以在不重绘按钮的情况下更新 UIButton 中的标签吗?

Can I update a label in a UIButton without redrawing the button?

我希望时钟读数在 UIButton 中每秒显示一次作为标签。但是即使我将它从 superView 中删除,新的 UIButton 也会覆盖旧的,就像这样 ... 几分钟后,我的 iPhone 看起来严重烧伤了:-)

PlayViewController 中,我的 NSTimer 方法如下所示

    - (void)startClock {    
        clockCount         =  0;       // start on 1st clock pulse
        totalMinutes       =  0;        // appears on clockButton
        totalSeconds       =  0;        // appears on clockButton

        timer = [NSTimer scheduledTimerWithTimeInterval:STATES_ConcertClock
                                             target:self
                                           selector:@selector(nextClock)
                                           userInfo:nil
                                            repeats:YES];
    }

    - (void)nextClock {

        self.lastEventChangeTime = [NSDate date];    
        clockCount++;
        [self masterClockReadout];    
    }

这是我的时钟读出方法

    - (void)masterClockReadout                              {
        totalMinutes       = clockCount / 60;
        totalSeconds       = clockCount % 60;
        clockString        = [NSString stringWithFormat:@"%02d:%02d", totalMinutes, totalSeconds];

        [self.seconds removeFromSuperview];
        EscButton *seconds = [[EscButton alloc] loadEscButton:(NSString *)clockString];
        [self.view addSubview:seconds];
    }

我还设置了一个 UIView 属性 所以 removeFromSuperview 知道要删除什么。

    @property (nonatomic, retain) UIView* seconds;

我的问题是:我可以在不重绘按钮的情况下更新 UIButton 标签吗?并且,这个问题可以通过使用 delegate 来解决吗?

到目前为止,我使用 delegates 的经验是将消息从 UIButton 发送到 ViewController(例如下面) 但到目前为止我还没有找到一个我可以应用的示例,其中消息以相反的方向发送。因此,如果使用 delegate 是推荐的方法,能否请您指出一些可能帮助我解决此问题的代码。谢谢

EscButton.h

    #import <UIKit/UIKit.h>

    @protocol EscButtonDelegate <NSObject>
    -(void)fromEscButton:(UIButton*)button;
    @end

    @interface EscButton : UIView {    
    }
    - (id)loadEscButton:(NSString *)text;
    @property (assign) id<EscButtonDelegate> delegate;
    @end

EscButton.m

    #import "EscButton.h"

    @implementation EscButton

    - (id)loadEscButton:(NSString *)text                          {

        CGFloat sideOffset   = screenWidth - ESC_BUTTON_Width - MARGIN_Side;
        CGFloat topOffset    = statusBarHeight + MARGIN_Top;

        UIButton *escButton  = [UIButton buttonWithType:UIButtonTypeCustom];
        escButton.frame      = CGRectMake(sideOffset, topOffset, ESC_BUTTON_Width, ESC_BUTTON_Height);

      //      etc …

        [escButton addTarget:self.delegate action:@selector(fromEscButton:) forControlEvents:UIControlEventTouchUpInside];
        [escButton setTitle:text forState:UIControlStateNormal];

      //       etc …

        return escButton;
    }

    @end

无需不断添加和删除按钮。你可以保留同一个。更改以下代码:

@property (nonatomic, retain) UIView* seconds;

至:

@property (nonatomic, retain) UIButton *secondsB;

同时更改:

[self.seconds removeFromSuperview];
EscButton *seconds = [[EscButton alloc] loadEscButton:(NSString *)clockString];
[self.view addSubview:seconds];

至:

if (!self.secondsB) {

    self.secondsB = [[EscButton alloc] loadEscButton:(NSString *)clockString];
    [self.view addSubview:_secondsB]; // previously addSubview:seconds]; 
}

[self.secondsB setTitle:clockString forState:UIControlStateNormal];