如何在Singleton中调用ViewController的UILabel

时间:2015-07-13 13:35:57

标签: ios objective-c iphone nstimer

我问了一个问题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来电LabTimerMySingleton有一个NSString来电display text我要打电话给LabTimer MySingleton,并在displaytext中显示LabTimer 我不知道怎么做?

2 个答案:

答案 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属性的赋值。我希望这很清楚。

解决您的问题:

  1. 向MySingleton添加对TimeViewController的弱引用:
  2. @property(弱)TimeViewController * myViewController;

    1. 在调用doSomethingWithString之前,将self指定给singleton
    2.   

      MySingleton * singleton = [MySingleton getInstance];   nowInterval =(NSTimeInterval)CountDown.countDownDuration; nowTimer =   nowInterval; singleton.myViewController = self; [单   doSomethingWithString:及(nowTimer)];

      1. 每当计时器触发时,调用TimeViewController的方法
      2.   

        ... displayText = [[NSString alloc] initWithFormat:@"%02u:%02u:   %02U",小时,分钟,秒]; [self.myViewController   updateDisplay:displayText];