[英]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. 我正在构建类似于Snapchat的应用程序,在该应用程序中我们拍摄照片或视频,然后经过一定时间后破坏。
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. 我在相机模块中实现了一个UIPicker,一旦用户拍照,他们就选择接收者看到图片的时间,然后用户会在1-10秒后发送自毁图像。
How can I link the UIPicker to the NSTimer which is another class? 如何将UIPicker链接到另一个类的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 file where image is displayed for the user: this image should destruct after the amount of time the user selects from the picker. image.m文件,其中为用户显示图像:在用户从选择器中选择了一段时间之后,该图像应被销毁。
-(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. 我需要让用户在选择器上选择一个时间,然后使image.m文件中的图像在该时间量后消失。
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 屏幕的一半是图片,另一半是UIPickerView
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 : 在ImageViewController.h
您需要这样的东西:
@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
然后,只需使用传递的seconds
属性值即可在camera.m
viewDidLoad
中为计时器计时
Why you don't just present the image view controller once the user select the the duration from the picker and pass the value of seconds as property of the destination view controller . 为什么当用户从选择器中选择持续时间并将秒值作为目标视图控制器的属性传递后,为什么不只显示图像视图控制器。 check Losiowaty answer 检查Losiowaty答案
If you have a special case for example the view controller that you want to inform is already created and it's not your current view controller that is responsible of creating and presenting it then NSNotificationCenter
will inform any object that has registered to observe the notification of selecting and sending the photo. 例如,如果您有特殊情况,则已经创建了要通知的视图控制器,而不是您当前的视图控制器负责创建和呈现该视图控制器,则NSNotificationCenter
将通知已注册的任何对象以观察选择和通知的通知。发送照片。 We can create a notification called for example didSendPhoto
and any object can observe and receive a notification once a user send a photo, we an pass data also in our case the duration. 我们可以创建一个名为didSendPhoto
的通知,一旦用户发送了照片,任何对象都可以观察并接收到通知,在这种情况下,我们还可以传递数据。
An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. NSNotificationCenter对象(或简称为通知中心)提供了一种在程序内广播信息的机制。 An NSNotificationCenter object is essentially a notification dispatch table. NSNotificationCenter对象本质上是一个通知调度表。
NSNotificationCenter Class Reference NSNotificationCenter类参考
So When the user select the amount of time and send the image you post a notification with the duration as userInfo
因此,当用户选择时间量并发送图像时,您会发布一条持续时间为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];
Now from where you want to scheduledTimerWithTimeInterval
add observer for the notification previously created 现在从您要scheduledTimerWithTimeInterval
为先前创建的通知添加观察者
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didSendImage:)
name:@"didSendPhoto"
object:nil];
then implement the method 然后实施方法
-(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]
}
You need to take property for storing the selected value from picker view 您需要使用属性来存储从选择器视图中选择的值
camera.h camera.h
#import "AppDelegate.h"
say at AppDelegate 在AppDelegate说
@property (readwrite, nonatomic) int secondsValue;
camera.m camera.m
ViewDidLoad Method 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 image.h的
#import "AppDelegate.h"
image.m image.m
appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
[NSTimer scheduledTimerWithTimeInterval:appDelegate.secondsValue
target:self
selector:@selector(timeout)
userInfo:nil
repeats:NO];
This can link Picker View selection to NSTimer..! 这可以将选择器视图选择链接到NSTimer。 Hope it helps you..! 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.