新建一个打印类 初始化代码如下
@implementation Print
-(id)init{
    self = [super init];
    if (self) {          [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(print) userInfo:nil repeats:YES];
          //每隔一秒 打印一次
      }
}
-(void)print{
    NSLog(@"=========================");
}@end@implementation PrintViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self AutoPrint];
}
-(void)AutoPrint{
   Print *p = [[Print alloc] init];
  //生成P对象
}-(void)StopPrint{// 想要在这里释放p对象,从内存里清除,代码应该怎么写?
}
@end

解决方案 »

  1.   

    执行上面的代码,startPrint调用一次,会一直打印1111。也就是说p是一直存在的啊。 想要停止,应该怎么弄?
      

  2.   

    我的理解是 p销毁了 init里的NSLog方法就不执行了吧。事实是NSLog 会一直输出。是不是说明p一直存在的。 而且,创建很多个p对象后,系统会变卡。  我想解决p可以销毁,系统不会卡的问题。
      

  3.   

    repeats:改成NO试试
      

  4.   

    或者你创建一个实力变量的NSTimer 在Stop里面执行timer的invalidate操作
      

  5.   

    你的PRINT类声明在AutoPrint函数中了,还怎么释放?
    把PRINT类声明在PrintViewController中, 在StopPrint中把p置为NULL不就行了
      

  6.   

    很多人都会忽略这个细节,NSTimer会持有target
    所以你的这句语句
     [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(print) userInfo:nil repeats:YES];
    会让NSTimer对self的计数器加1,所以你的p不会被释放,直到你的NSTimer计时器被显式的设置无效.
    也就是你要调用[NSTimer invalidate]才会释放p,就象5楼说的