检测UIDatePicker是否正在滚动?

时间:2011-02-20 06:50:34

标签: iphone objective-c uidatepicker

我在模态视图中使用UIDatePicker来选择日期。我发现如果视图在日期选择器仍在滚动时关闭,我会得到一个EXC_BAD_ACCESS。

当用户选中按钮时,如何检测日期选择器正在滚动?

我有两个想法:

1)检测值是否已经改变:

[myDatePicker addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];

但我需要能够检测值是否会改变。

2)检查日期是否有效,但是当日期选择器滚动时,返回上一个日期,当然有效。

有什么想法吗?

5 个答案:

答案 0 :(得分:11)

为了避免这种崩溃,您不需要尝试预测选择器何时尝试向您的类发送操作方法调用,而只需在ViewController的dealloc例程中删除选择器的事件操作。 / p>

编辑:oops,正如评论中指出的那样,UIDatePicker没有委托。但是你可以为它分配动作事件。因此,应该删除任何指向正在dealloc'd的对象的动作集。

- (void dealloc
{
    [myPicker removeTarget:self action:... forControlEvents:...];
    [myPicker release];
    [super dealloc];
}

答案 1 :(得分:4)

我知道我已经很晚了,但我刚刚用委托创建了一个自定义的UIDatePicker子类:

这是代表:

// In TDDatePickerDelegate.h

#import <Foundation/Foundation.h>
#import "TDDatePicker.h"

@class TDDatePicker;
@protocol TDDatePickerDelegate

@required
- (void)datePickerChanged:(TDDatePicker *)datePicker newDate:(NSDate *)newDate;

@end

这是子类

// In TDDatePicker.h

#import <UIKit/UIKit.h>

#import "TDDatePickerDelegate.h"
@interface TDDatePicker : UIDatePicker

- (void)setHidden:(BOOL)hidden animated:(BOOL)animated;
- (void)dateChanged;

@property (nonatomic, assign) IBOutlet id <TDDatePickerDelegate> delegate;

@end



// In TDDatePicker.m

#import "TDDatePicker.h"

@implementation TDDatePicker

@synthesize delegate;

- (id)init {
    self = [super init];
    if (self) {
    [self addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
}
return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
    }
    return self;
}
- (void)dateChanged {
    [delegate datePickerChanged:self newDate:self.date];
}

- (void)dealloc {
    [self removeTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
}

@end

答案 2 :(得分:1)

目标-C:

[datePicker addTarget:self action:@selector(isRolled) forControlEvents:UIControlEventValueChanged];

- (void)isRolled {
     NSLog(@"Date Picker has been rolled.");
}

夫特:

datePicker.addTarget(self, action: "isRolled", forControlEvents: .ValueChanged)

func isRolled() {
    print("Date Picker has been rolled.")
}

答案 3 :(得分:0)

正式地说,您无法使用 Apple的官方UIDatePicker方法检索这些条款中的选择器状态。

如果有解决方案,我很想看到它们。

答案 4 :(得分:0)

有一个技巧可以检测到这个但是没有委托方法/属性来检测它是否滚动

  1. 以isScrolling
  2. 取得财产
  3. func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?或等效方法
  4. 中将isScrolling设置为true
  5. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
  6. 中将isScrolling设置为true
  7. 仅当选择器已达到滚动禁用的有限状态时,才启用模态视图的关闭按钮
  8. 希望这会有所帮助

相关问题