【发布时间】: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 的经验是从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
【问题讨论】:
标签: objective-c uiview uibutton delegates nstimer