gpt4 book ai didi

ios - NSTimer 与 UIPickerView iOS 链接

转载 作者:行者123 更新时间:2023-11-29 12:17:17 25 4
gpt4 key购买 nike

我有一个 UIPickerView,在一个 View Controller 中有一个数组,在另一个 View Controller 中有一个 NSTimer,我正在尝试:

1) 将选择器数组链接到 NSTimer,以便用户在选择器上选择一个时间让图像自毁

2) 在 UILabel 中显示剩余时间,以便用户选择下降到 0 的秒数。(例如,从选择器标签中选择 2 显示 2 然后 1 然后 0 和图像 destucts)

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];
}

NSTimer 不响应 self.seconds 并且不从 UIPicker 获取用户输入。

关于如何将选择器链接到计时器以便计时器响应用户在选择器中选择的秒数并在标签中显示剩余秒数的任何想法?

我的 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];
}
}

它可能有什么问题,onviewcontroller 方法有效,无论我传递什么它都有效,但对于 tvc.seconds 没有任何传递给它。

最佳答案

您正在创建 Controller 的一个新实例,而不是将要使用的实际实例。您应该使用 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

关于ios - NSTimer 与 UIPickerView iOS 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31699667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com