【问题标题】:NSTimer crashesNSTimer 崩溃
【发布时间】:2011-11-23 15:28:00
【问题描述】:

我一直在寻找答案,但没有任何效果。

我有一个 NSTimer 作为 Appdelegate 的属性,这个 NSTimer 应该在任何时候触发他的动作,即使应用程序在后台(它是一个本地化应用程序 - 所以它可以永远运行)

这是代码:

if(conectar){
            self.myTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
                                                            target:self
                                                          selector:@selector(abrirPresencia)
                                                          userInfo:nil
                                                           repeats:NO]retain];
        }
        else{
            self.myTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
                                                    target:self
                                                  selector:@selector(cerrarPresencia)
                                                  userInfo:nil
                                                           repeats:NO]retain];
        }

应用程序崩溃 10 秒后,我在两个方法中都有断点并且它不会停止,就好像该方法甚至没有被调用,而不是触发它崩溃的方法。

方法如下:

-(void)cerrarPresencia{
NSLog(@"SOY EL TIMERRRRR!!!! %@");
//[[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground
if (YES){
    [self.location stopUpdatingLocation];
    [self disconnect];

}
else{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ups!"
                                                        message:@"Según tu perfil querrías estar desconectado pero como lo estás usando hemos incluido este horario. Para cambiarlo sólo tienes que ir a editar perfil."
                                                       delegate:self 
                                              cancelButtonTitle:@"Ok" 
                                              otherButtonTitles:nil];
    [alertView show];

    [alertView release];
}

}

-(void)abrirPresencia{
NSLog(@"SOY EL TIMERRRRR!!!! %@");
if (YES){
    [self.location stopUpdatingLocation];
    [self disconnect];

}
else{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Ups!"
                                                        message:@"Según tu perfil querrías estar desconectado pero como lo estás usando hemos incluido este horario. Para cambiarlo sólo tienes que ir a editar perfil."
                                                       delegate:self 
                                              cancelButtonTitle:@"Ok" 
                                              otherButtonTitles:nil];
    [alertView show];

    [alertView release];
}

}

属性是:

@property (nonatomic, retain)NSTimer *myTimer;

有什么想法吗?

【问题讨论】:

  • 你能发布这些方法吗?崩溃日志中有什么错误?
  • 如果你调用的方法有一个(或多个)参数,你应该在选择器的末尾添加一个冒号:。你能按照@Vladimir 的要求做并发布方法吗?
  • 很可能这是一个 SIGABRT。尝试在选择器名称中添加“:”。例如:(cerrarPresencia:)
  • 能否也粘贴 myTimer 的属性?

标签: objective-c


【解决方案1】:
NSLog(@"SOY EL TIMERRRRR!!!! %@");

记住你在这一行得到的编译器警告说

警告:转换次数多于数据参数 [-Wformat,7]

?

注意这些警告。您使用包含格式说明符%@ 的参数调用NSLog,这使它认为还会有更多参数。如果您不传递这些进一步的参数,该函数仍会在它期望参数所在的位置查找,找到垃圾并导致崩溃。

【讨论】:

  • 那位先生,看得很清楚! @David Shaikh,尝试从这两种方法中删除 %@ 行,看看会发生什么。如果您仍然遇到崩溃,请包含崩溃日志。
  • 天哪!就是这样!感谢大家,特别是@Josh Caswell
【解决方案2】:

选择器错误,一定是这种形式:

- (void) abrirPresencia:(NSTimer*)theTimer

选择器还需要指定一个带有尾随冒号的参数: selector:@selector(abrirPresencia)

来自 Apple API 文档:

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

定时器触发时发送给目标的消息。选择器必须具有以下签名:

- (void)timerFireMethod:(NSTimer*)theTimer

【讨论】:

  • 我添加了您建议的更改 -(void)cerrarPresencia:(NSTimer*)theTimer{ selector:@selector(cerrarPresencia:) 我得到了同样的错误....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-25
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 2011-04-11
  • 2022-11-02
  • 1970-01-01
相关资源
最近更新 更多