0

我一直在开发一个只包含两个元素 aUILabel和 a的小应用程序UIPickerView

UILabel更新 以NSTimer从 60 倒计时到 0。

仅包含一个数据数组,UIPickerView不直接与UILabelwith交互NSTimer

我的问题是,当我玩UIPickerView并且向上或向下滚动时,计数器“动画”停止,继续执行其操作,直到您从 UIPickerView 中选择一个选项。

我无法解决这个问题,我不知道这是否是 iPhone SDK 的错误,或者是否有任何属性作为 becomeFirstResponder 可以帮助我。

我正在使用 iOS SDK 6.1。

这是我的代码:

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>{
    IBOutlet UIPickerView *uno;
    NSMutableArray *numeros;
    NSTimer *timer;
    IBOutlet UILabel *contador;
    int num;
}

@property (nonatomic, retain) IBOutlet UIPickerView *uno;

@property (nonatomic, retain) NSMutableArray *numeros;

@property (nonatomic, retain) IBOutlet UILabel *contador;

@end

视图控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize uno, numeros, contador;

- (void)viewDidLoad
{
    [super viewDidLoad];

    numeros = [[NSMutableArray alloc] init];
    [numeros addObject:@"0"];
    [numeros addObject:@"1"];
    [numeros addObject:@"2"];
    [numeros addObject:@"3"];
    [numeros addObject:@"4"];
    [numeros addObject:@"5"];
    [numeros addObject:@"6"];
    [numeros addObject:@"7"];
    [numeros addObject:@"8"];
    [numeros addObject:@"9"];

    uno.delegate = self;
    uno.dataSource = self;

    num = 60;

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [numeros count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [numeros objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"Hi");
}

- (void)updateCounter:(NSTimer *)theTimer {
    num = num - 1;
    [contador setText:[NSString stringWithFormat:@"%d",num]];
}

@end

该应用程序如下所示:http: //imgur.com/ljB93tN

4

1 回答 1

1

add NSRunLoop after

timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];


NSRunLoop *runner = [NSRunLoop currentRunLoop];
        [runner addTimer:timer forMode: NSDefaultRunLoopMode];
于 2013-02-14T09:53:26.677 回答