Link UIPickerView 到 NSTimer iOS

Link UIPickerView to NSTimer iOS

我正在构建一个类似于 snapchat 的应用程序,我们可以在其中拍摄照片或视频,然后它会在一定时间后销毁。

我在相机模块中实现了一个 UIPicker,一旦用户拍照,他们会 select 收件人看到照片的时间,然后用户发送的图像会在 1- 10 秒。

如何将 UIPicker link 连接到另一个 class 的 NSTimer?

camera.h

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


picker data in camera.m file:
interface //
{
 int secs;
}
@end

- (void)viewDidLoad {
    [super viewDidLoad];

    self.pickerData = @[@"1 second", @"2 seconds", @"3 seconds", @"4 seconds", @"5 seconds", @"6 seconds", @" 7 seconds", @" 8 seconds", @" 9 seconds", @" 10 seconds"];

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

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

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

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

 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
            {
               //help here please
            }
- (IBAction)next:(id)sender {
//goes to select friends page
}

image.m 为用户显示图像的文件:该图像应在用户从选择器中 select 秒后销毁。

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

    if ([self respondsToSelector:@selector(timeout)]) {

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

    }

}

[timeout pops to root view]

我需要让用户 select 在选择器上停留一段时间,然后 image.m 文件中的图像在这段时间后销毁。

用户拍照然后图片下方是选择器查看用户select一次并按下一步转到select朋友。屏幕的一半是图片另一半是 UIPickerView

只需将数据传递给第二个视图控制器,即显示图像的视图控制器。 像这样:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // leave this empty

}

- (void)buttonHandler:(UIButton *)sender {
    image *iVC = [[image alloc] init];
    iVC.seconds = [self.timePicker selectedRowInComponent:0];

    [self presentViewController:iVC animated:YES];
}

而在 ImageViewController.h 中,您需要这样的东西:

@interface image : UIViewController

@property (nonatomic, assign) NSUInteger seconds;

@end

然后只需使用 seconds 属性 中传递的值在 viewDidLoad camera.m

中为您的计时器计时

为什么不在用户 select 选择器的持续时间后才显示图像视图控制器,并将秒值作为目标视图控制器的 属性 传递。

如果您有特殊情况,例如您要通知的视图控制器已经创建,并且不是您当前的视图控制器负责创建和呈现它,那么 NSNotificationCenter 将通知任何对象已注册观察select发送照片的通知。我们可以创建一个名为 didSendPhoto 的通知,一旦用户发送照片,任何对象都可以观察和接收通知,在我们的例子中,我们也会传递持续时间。

An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table.

NSNotificationCenter Class Reference

因此,当用户 select 的时间量和向您发送图像时 post 持续时间为 userInfo

的通知
 // instead 50 put the choice of the user from the picker 
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@(50) forKey:@"durationBySeconds"];
 // post the notification 
[[NSNotificationCenter defaultCenter] postNotificationName: @"didSendPhoto"
                                                    object:nil
                                                  userInfo:userInfo];

现在从您想 scheduledTimerWithTimeInterval 为之前创建的通知添加观察者

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didSendImage:)
                                             name:@"didSendPhoto"
                                           object:nil];

然后实现方法

-(void) didAddReview:(NSNotification *) notification
{
    NSNumber *seconds = (NSNumber *)[notification.userInfo objectForKey:@"durationBySeconds"];


  [NSTimer scheduledTimerWithTimeInterval:  [seconds integerValue]
                         target:self
                       selector:@selector(timeout)
                       userInfo:nil
                        repeats:NO];
     [timeout method pops to rootview]

}

您需要 属性 来存储从选择器视图中选择的值

camera.h

#import "AppDelegate.h"

在 AppDelegate 发言

@property (readwrite, nonatomic) int secondsValue;

camera.m

ViewDidLoad 方法

appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];


-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
       //help here please
       appDelegate.secondsValue=(int)[self.pickerData objectAtIndex:row];

    }

image.h

#import "AppDelegate.h"

image.m

appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];



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

这可以link Picker View 选择到NSTimer..! 希望对你有帮助..!