【问题标题】:Help changing NSTimer for textLabel. Code included帮助更改 textLabel 的 NSTimer。包含代码
【发布时间】:2010-11-11 00:01:51
【问题描述】:

想将我的 NSTimer 代码更改为使用 applicationSignificantTimeChange

这就是我现在拥有的。

基本上我想更改计时器,而不是每 5 秒更改一次,例如我希望这些 textLabels 在每晚 12 点更改,有人帮帮我吗?

//h file

NSTimer *timer;

IBOutlet UILabel *textLabel;

//m file

- (void)onTimer {

    static int i = 0;

    if ( i == 0 ) { 
        textLabel.text = @"iphone app";
    }

    else if ( i == 1 ) {
        textLabel.text = @" Great App!";
    }

    else if ( i == 2 ) {
        textLabel.text = @" WOW!";
    }

    else {
        textLabel.text = @" great application again!!";
        i = -1;
    }

    i++;
}


timer =[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 

【问题讨论】:

    标签: iphone xcode


    【解决方案1】:

    您可以做两件事来响应 applicationSignificantTimeChange:

    (1) 如果您与应该更新的视图控制器有连接,只需在您的应用委托中实现 applicationSignificantTimeChange:。

    - (void)applicationSignificantTimeChange:(UIApplication *)application {
      yourViewController.textLabel.text = @"random text";
    }
    

    (2) 在应该获取更新的视图控制器中订阅 UIApplicationSignificantTimeChangeNotification 通知。您也许可以将该代码放入 viewDidLoad

    - (void)viewDidLoad {
        [super viewDidLoad];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(onSignificantTimeChange:)
                                                     name:UIApplicationSignificantTimeChangeNotification 
                                                   object:nil];
    }
    
    - (void)onSignificantTimeChange:(NSNotification *)notification {
        self.textLabel.text = @"random text";
    }
    

    你还需要调用

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    不再需要您的视图控制器时的某个地方。

    【讨论】:

    • 我比我的回答更喜欢这个。如果 iPhone 可以通知你时间,那就再好不过了!
    【解决方案2】:

    计时器的选择器应该是:

    -(void) onTimer:(NSTimer *) timer
    {
       // yourcode
    
    }
    

    并调用:

    [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
    

    你需要 "onTimer*:*"


    [编辑]

    要调用将在凌晨 12 点关闭的函数,对于低性能(即不是 12:00:00:0000),您可以像在后台运行的应用一样执行以下操作:

    NSTimeInterval minute = 60.0f
    [NSTimer scheduledTimerWithTimeInterval:minute target:self selector:@selector(onTimer:) userInfo:nil repeats:YES]
    
    -(void) onTimer:(NSTimer *) timer
    {
       NSDate *midnight = [NSDate dateWithNaturalLanguageString:@"today at 12:00 am"]; // unsure of this line
       if ([midnight timeIntervalSinceDate:[NSDate date]] < 30.0f)
          // set off alarm.
    
    }
    

    【讨论】:

    • 使用“onTimer”我应该把上午 12 点放到 TimeInterval 的哪个位置?
    • 在上面添加了一些修改。未经测试的代码,请注意。您将不得不摆弄时间间隔,并且可能使用“今天晚上 11:59”而不是午夜。
    • dateWithNaturalLanguageString: 在 iphone 上不可用(如“不应使用”)。
    • 疯了。那你就得用别的了,抱歉。大量的字符串解析或从 NSDate 获取“DAY”并提供当前时间的东西。
    【解决方案3】:

    对我来说似乎很简单——只需为午夜创建一个 NSDate,以秒为单位计算 24 小时的时间间隔,然后使用
    - (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

    创建 NSTimer

    【讨论】:

    • 任何例子都会有帮助
    猜你喜欢
    • 2011-07-04
    • 2011-04-26
    • 2011-07-12
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多