0

I've been working on a small app that contains only two elements, a UILabel and a UIPickerView.

The UILabel is updated by an NSTimer to count down from 60 to 0.

The UIPickerView only contains an array of data, don't interact directly with either UILabel with NSTimer.

My problem is that when I play with the UIPickerView and I scroll up or down, the counter "animation" stops, continues to make its action until you select an option from the UIPickerView.

I could not fix this, I do not know if it's a bug with the iPhone SDK or is there any property as becomeFirstResponder that can help me.

I'm using iOS SDK 6.1.

This is my code:

ViewController.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

ViewController.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

The app looks like this: https://i.stack.imgur.com/6pWpH.jpg

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
Rigter
  • 27
  • 1
  • 5

1 Answers1

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];
NANNAV
  • 4,875
  • 4
  • 32
  • 50