【问题标题】:Link UIPickerView to NSTimer iOS将 UIPickerView 链接到 NSTimer iOS
【发布时间】:2015-07-23 14:41:29
【问题描述】:

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

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

如何将 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 文件:此图像应在用户从选择器中选择的时间后销毁。

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

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

用户拍照然后图片下方是用户选择时间然后按next去选择朋友的pickerview。屏幕一半是图片另一半是UIPickerView

【问题讨论】:

  • 这两个类如何连接的信息很少,很难说。不知何故,pickerView didSelectRow ... 应该更新image.m 中的信息。这是基于我在这里看到的几行代码的猜测。
  • 所以我想链接到 NStimer 的选择器数据(秒),那么我们将在选择行方法中放入什么代码
  • @Harry 您没有提供足够的信息,但我想NSNotificationCenter 可以提供帮助,请查看我的答案
  • @Aladin 请看上面我已经提供了我已经实现的所有内容
  • 选择时间后是否有类似done的确认按钮?因为每次用户滚动选择器时都会触发didSelectRow

标签: ios objective-c nsarray uipickerview nstimer


【解决方案1】:

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

-(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 属性中传递的值来为viewDidLoadcamera.m 中的计时器计时

【讨论】:

  • 我已经添加了我上面实现的所有内容
  • 已编辑 - 您需要的一切都在那里。
  • didSelectRow 每次用户滚动选择器时都会触发,只要用户滚动到 2 秒,您就会显示 ImageViewController
  • 用户拍照,然后在图片下方的选择器上选择时间,然后按next选择好友
  • 这个答案应该有效,但将代码移动到“下一步”按钮的操作并从选择器中获取所选值,如果您想通知并发送金额,我的答案仍然有效秒到超过一个视图控制器
【解决方案2】:

您需要从选取器视图中获取用于存储所选值的属性

相机.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];

    }

图像.h

#import "AppDelegate.h"

图像.m

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



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

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

【讨论】:

  • 通过 AppDelegate 在视图控制器之间传递值是一个坏主意,不应该这样做。这不是 AppDelegate 的用途。
  • 如果您想在两个不相关的视图之间传递数据,上述方法很有用。他没有提供有关他正在使用的相机和图像对象的信息。所以,我是这样回答的。他可以将这个想法与在他的视图中获取的属性一起使用,而不是 AppDelegate 也......!
  • 不是NSNotificationCenter 比通过AppDelegate 在视图控制器之间传递数据更好吗?
  • 可能……更好。不过觉得这种方式比使用NSNotificationCenter更容易理解……
  • 是的,但是学习不良的编码习惯会非常糟糕。它降低了代码质量并增加了维护成本。
【解决方案3】:

当用户从选择器中选择持续时间并将秒的值作为目标视图控制器的属性传递时,为什么不只显示图像视图控制器。 check Losiowaty answer

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

一个 NSNotificationCenter 对象(或简称为通知中心) 提供了一种在节目中广播信息的机制。一个 NSNotificationCenter 对象本质上是一个通知调度 表。

NSNotificationCenter Class Reference

因此,当用户选择时间量并发送图像时,您会发布通知,持续时间为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]

}

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多