【问题标题】:Can I update a label in a UIButton without redrawing the button?我可以在不重绘按钮的情况下更新 UIButton 中的标签吗?
【发布时间】:2017-06-09 07:45:41
【问题描述】:

我希望时钟读数以标签形式出现在 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 的经验是从UIButtonViewController 发送消息(例如下面)但到目前为止我还没有找到我能够做到的示例适用于以相反方向发送消息的地方。因此,如果使用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

【问题讨论】:

    标签: objective-c uiview uibutton delegates nstimer


    【解决方案1】:

    无需不断添加和删除按钮。您可以保留相同的。更改以下代码:

    @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];
    

    【讨论】:

    • 你的意思是[self.view addSubview:secondsB]; ?
    • RunLoop,当我更改我评论的行时,这解决了我的问题。我同意你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多