我有一个UIPickerView数组,我希望链接到NSTimer。
in quiz.h file
@property (strong, nonatomic) IBOutlet UIPickerView *timePicker;
我在quiz.m文件中有数组:
- (void)viewDidLoad {
[super viewDidLoad];
self.pickerData = @[@"1 second", @"2 seconds", @"3 seconds", @"4 seconds", @"5 seconds"];
self.timePicker.dataSource = self;
self.timePicker.delegate = self;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[self.pickerData objectAtIndex:[self.timePicker selectedRowInComponent:0]];
}
我希望将这个秒数链接到imageview.h文件中的属性
@property (nonatomic, assign) int seconds;
我怎么能在quiz.m文件中做到这一点?
我正在尝试:
ImageViewController *ivc = [[ImageViewController alloc] init];
ivc.seconds = [self.timePicker selectedRowInComponent:0]; // warning displays conversion loses integer precision
我在ivc.seconds中添加什么来将它链接到选择器数组并在选中时获取每行中的数字秒数?
我想将秒数与NSTimer相关联。
[NSTimer scheduledTimerWithTimeInterval:self.seconds target:self selector:@selector(timeout)
userInfo:nil repeats:NO];
超时方法弹出到rootview
答案 0 :(得分:0)
制作self.pickerData = @[@"1", @"2", @"3", @"4", @"5"];
和选择方法,
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
int value = [[self.pickerData objectAtIndex:row] intValue];
NSString *secondString = (value > 1) ? @"seconds" : @"second"
NSString *title = [NSString stringWithFormat:@"%@ %@", [self.pickerData objectAtIndex:row], secondString];
return title;
}
并创建
NSInteger row = [self.timePicker selectedRowInComponent:0];
ImageViewController *ivc = [[ImageViewController alloc] init];
ivc.seconds = [[self.pickerData objectAtIndex:row] intValue];
显示倒计时器,先设置时间
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(setTimeToLabel) userInfo:nil repeats: YES];
然后在setTimeToLabel
方法
-(void) setTimeToLabel
{
if (self.seconds != 0) {
self.timeLabel.text = [NSString stringWithFormat:@"%d", self.seconds];
self.seconds = self.seconds - 1;
} else {
[timer invalidate];
}
}