我问了一个问题:link
在讨论了这个问题之后,我将重写原始程序,成为Singleton
方法。
我会调用计时器以哪种方式写入MySingleton
,然后在TimerViewController
内写入MySingleton
的方法:
MySingleton.h
@interface MySingleton : NSObject
{
int remainder;
NSTimer *timer;
NSTimeInterval countDownInterval;
int hours;
int mins;
int secs;
}
@property(nonatomic, retain)NSString *displayText;
@property int afterRemainder;
+(MySingleton*) getInstance;
-(void)doSomethingWithString:(int*)parameter;
@end
MySingleton.m
-(void)doSomethingWithString:(int*)parameter {
remainder = *parameter;
_afterRemainder = *parameter - remainder % 60 ;
timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCountDown)userInfo:nil repeats:YES];
}
-(void)updateCountDown{
_afterRemainder --;
hours = (int)(_afterRemainder/(60*60));
mins = (int)(((int)_afterRemainder/60)-(hours * 60));
secs = (int)(((int)_afterRemainder - (60 * mins) - (60 * hours *60)));
displayText = [[NSString alloc]initWithFormat:@"%02u : %02u : %02u",hours,mins,secs];
}
TimerViewController.h
@interface TimerViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIDatePicker *CountDown;
@property (retain, nonatomic) IBOutlet UILabel *LabTimer;
@property (retain, nonatomic) IBOutlet UIButton *BtnStart;
- (IBAction)StartCmd:(id)sender;
@end
TimerViewController.m
此处要调用计时器,UILabel
位于TimerViewController.m
- (IBAction)StartCmd:(id)sender {
MySingleton* singleton = [MySingleton getInstance];
nowInterval=(NSTimeInterval)CountDown.countDownDuration;
nowTimer = nowInterval;
[singleton doSomethingWithString:&(nowTimer)];
}
TimerViewContorller
有一个Label
来电LabTimer
,MySingleton
有一个NSString
来电display text
我要打电话给LabTimer
MySingleton
,并在displaytext
中显示LabTimer
我不知道怎么做?
答案 0 :(得分:0)
不明白你的问题。但请告诉我单身人士是如何工作的。
这样:
你有一些Singleton类:MySingleton
。
h.file
@interface MySingleton : NSObject {
}
+ (id)sharedManager;
- (void)SomeMethod:(int)someParameter;
@end
m file
@implementation MySingleton
+ (id)sharedManager {
static MySingleton *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
#pragma mark - Some public Method
- (void)SomeMethod:(int)someParameter
{
// code here
[self somePrivateMethod];
}
#pragma mark - Some private Method
- (void)somePrivateMethod {
// code here
}
你可以在MainClassController
:
// some code before
/* call singleton `Some Method` */
[[MySingleton sharedManager] SomeMethod:'int value'];
// code after
也许它有助于你。祝你好运)
答案 1 :(得分:0)
您需要在计时器触发时设置UILabel的文本。移动赋值语句,将displayText分配给计时器的操作方法(updateCountDown
)。
顺便说一句,我不明白UILabel.text = ...我认为这是代码中的语法错误。
当你做这样的作业时:
myLabel.text = displayText;
会发生什么情况是评估右侧表达式(在这种情况下是单个变量),并且在执行语句时将其值分配给UILabel的文本属性(并且用户可以看到它)。如果稍后更改displayText的值,那将对用户看到的内容没有任何影响 - 必须再次执行对text属性的赋值。我希望这很清楚。
解决您的问题:
@property(弱)TimeViewController * myViewController;
MySingleton * singleton = [MySingleton getInstance]; nowInterval =(NSTimeInterval)CountDown.countDownDuration; nowTimer = nowInterval; singleton.myViewController = self; [单 doSomethingWithString:及(nowTimer)];
... displayText = [[NSString alloc] initWithFormat:@"%02u:%02u: %02U",小时,分钟,秒]; [self.myViewController updateDisplay:displayText];