简体   繁体   English

NSTimer与UIPickerView iOS的链接

[英]NSTimer link with UIPickerView iOS

I have a UIPickerView with an array in one view controller and an NSTimer in another, i am trying to: 我有一个UIPickerView ,在一个视图控制器中有一个数组,在另一个视图控制器中有一个NSTimer ,我正在尝试:

1) link the picker array to NSTimer so a user selects a time on the picker for image to self destruct 1)将选择器数组链接到NSTimer,以便用户在选择器上选择一个时间以使图像自毁

2) show time remaining in UILabel so seconds the user selects going down to 0. (ex. select 2 from picker label shows 2 then 1 then 0 and image destucts) 2)显示UILabel中剩余的时间,因此用户选择秒数降至0。 (例如,从选择器标签中选择2显示2,然后是1,然后是0,然后显示图像)

one vc.h

@property (strong, nonatomic) IBOutlet UIPickerView *timePicker;
@property (nonatomic, strong) NSArray *pickerData;

one vc.m 

- (void)viewDidLoad {
    [super viewDidLoad];

    self.pickerData = @[@1,@2,@3,@4,@5,@6,@7];

    self.timePicker.dataSource = self;
    self.timePicker.delegate = self;
}

- (void) viewWillAppear:(BOOL)animated {

    [self.textField setText:self.userText];
}

-(long)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(long)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.pickerData.count;
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    [self.pickerData objectAtIndex:[self.timePicker selectedRowInComponent:0]];
}


two vc.h

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (nonatomic, assign) int seconds;

two vc.m

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

            [NSTimer scheduledTimerWithTimeInterval:7 target:self selector:@selector(timeout)
                                           userInfo:nil repeats:NO];

    [NSTimer scheduledTimerWithTimeInterval:self.seconds target:self selector:@selector(setTimeToLabel) userInfo:nil repeats:NO];
}

-(void)timeout
{
 // pops to root view
}


- (void)setTimeToLabel
{  
    self.seconds = self.seconds - 1;
    self.timeLabel.text = [NSString stringWithFormat:@"%d", self.seconds];
}

the NSTimer does not respond to the self.seconds and does not pick up the user input from the UIPicker . NSTimer不会响应self.seconds ,也不会从UIPicker接收用户输入。

any ideas as to how I can link the picker to the timer so that the timer responds to the seconds the user selects in the picker and display the remaining seconds in the label?? 关于如何将选择器链接到计时器的任何想法,以便计时器响应用户在选择器中选择的秒并在标签中显示剩余的秒?

my prepareforsegue method below: 我的prepareforsegue方法如下:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([segue.destinationViewController isKindOfClass:[oneViewController class]]) {
        oneViewController * ovc = (oneViewController*)segue.destinationViewController;
        ovc.array = //whatever;
    }

    if  ([segue.destinationViewController isEqual:@"secondVC"]) {
            NSInteger row = [self.timePicker selectedRowInComponent:0];
            twoViewController *tvc = [segue destinationViewController];
            tvc.seconds = [[self.pickerData objectAtIndex:row] intValue];
    }
}

what can be the issue with it, the onviewcontroller method works, whatever i pass it works, but for the tvc.seconds nothing is passed to it. 可能是什么问题,无论我通过了什么,onviewcontroller方法都可以工作,但是对于tvc.seconds,什么都没有传递给它。

You are creating a new instance of the controller rather than the actual one that would be used. 您正在创建控制器的新实例,而不是将要使用的实际实例。 You should be using the prepareForSegue method to do this, and get a reference to the destinationViewController, like so: 您应该使用prepareForSegue方法执行此操作,并获得对destinationViewController的引用,如下所示:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqual: @"nextScreen"]) {
        NSInteger row = [self.timePicker selectedRowInComponent:0];
        SecondViewController *tvc = [segue destinationViewController];
        tvc.seconds = [[self.pickerData objectAtIndex:row] intValue];
    }
}

// First View Controller
//.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIPickerView *timePicker;
@property (nonatomic, strong) NSArray *pickerData;
@property (weak, nonatomic) IBOutlet UIButton *next;

@end

//.m
#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.pickerData = @[@1,@2,@3,@4,@5,@6,@7];

    self.timePicker.dataSource = self;
    self.timePicker.delegate = self;
}

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

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

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.pickerData.count;
}

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat: @"%@", self.pickerData[row]];
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    [self.pickerData objectAtIndex:[self.timePicker selectedRowInComponent:0]];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqual: @"nextScreen"]) {
        NSInteger row = [self.timePicker selectedRowInComponent:0];
        SecondViewController *tvc = [segue destinationViewController];
        tvc.seconds = [[self.pickerData objectAtIndex:row] intValue];
    }
}

@end

// SecondViewController
//.h
#import "ViewController.h"

@interface SecondViewController : ViewController

@property (nonatomic) int seconds;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end


//.m
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [NSTimer scheduledTimerWithTimeInterval:7 target:self selector:@selector(timeout)
                                   userInfo:nil repeats:NO];

    [NSTimer scheduledTimerWithTimeInterval:self.seconds target:self selector:@selector(setTimeToLabel) userInfo:nil repeats:NO];
}

-(void)timeout
{
    // pops to root view
}


- (void)setTimeToLabel
{
    self.seconds = self.seconds - 1;
    self.timeLabel.text = [NSString stringWithFormat:@"%d", self.seconds];
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM