0

我是 iPhone 编程的新手。谁能告诉我如何使用分段控制器在这里设置时间?在下面的代码中,我给出了 0.5 时间,但现在我想使用段控制器设置时间。段控制器中的示例我采用了这样的 0.1、0.2、0.5、0.10、0.20、0.30,如果我通过段控制器选择任何一个数字,如 0.10,它会设置该数字代替 0.5。我怎样才能做到这一点?

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
                 target: self
                 selector: @selector(methodName:)
                 userInfo: nil
                 repeats: YES];
4

2 回答 2

0

一旦你选择了新的值,invalidate旧的计时器并开始一个新的。

于 2012-12-04T07:31:25.107 回答
0

假设对于段控制器中的示例,您采用这样的 0.1,0.2,0.5,0.10,0.20,0.30 值,然后见下文..

-(IBAction) segmentedControlIndexChanged{

  switch (self.segmentedControl.selectedSegmentIndex) {
    case 0:
      NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
             target: self
             selector: @selector(methodName:)
             userInfo: nil
             repeats: YES];
      break;
    case 1:
      NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 0.2
             target: self
             selector: @selector(methodName:)
             userInfo: nil
             repeats: YES];
      break;
     case 2:
      NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
             target: self
             selector: @selector(methodName:)
             userInfo: nil
             repeats: YES];
      break;
     case 3:
      NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 0.10
             target: self
             selector: @selector(methodName:)
             userInfo: nil
             repeats: YES];
      break;
     case 4:
      NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 0.20
             target: self
             selector: @selector(methodName:)
             userInfo: nil
             repeats: YES];
      break;
    default:
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 0.30
             target: self
             selector: @selector(methodName:)
             userInfo: nil
             repeats: YES];
       break;

   }
}
于 2012-12-04T07:34:45.363 回答