Search code examples
iosobjective-cnsarrayuipickerviewnstimer

Link UIPickerView to NSTimer iOS


I am building an app similar to snapchat where we take a picture or video and then it destructs after a certain amount of time.

I am implementing a UIPicker within the camera module and once the user take a picture they select a time for how long the recipient sees the picture, then the user sends the image that self destructs after 1-10 seconds.

How can I link the UIPicker to the NSTimer which is another class?

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 file where image is displayed for the user: this image should destruct after the amount of time the user selects from the picker.

-(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]

I need to let the user select a time on the picker and then for the image in the image.m file to destruct after that amount of time.

The user take a picture then below the picture is the pickerview the user selects a time and presses next to go to select friends. half of the screen is the picture the other half is the UIPickerView

enter image description here


Solution

  • Just pass the data to the second view controller, the one displaying the image. Something like this :

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

    And in ImageViewController.h you'd need something like this :

    @interface image : UIViewController
    
    @property (nonatomic, assign) NSUInteger seconds;
    
    @end
    

    Then just use the value passed in seconds property to time your timer in viewDidLoad in camera.m