Detect if UIDatePicker is rolling?

16,152

Solution 1

In order to avoid this crash, you don't need to try to predict when the picker will try to send an action method call to your class, instead, you simply remove the picker's event action in your ViewController's dealloc routine.

EDIT: oops, as was pointed out in a comment, the UIDatePicker doesn't have a delegate. But you can assign it action events. So instead, any actions set pointing to the object that is being dealloc'd should be removed.

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

Solution 2

I know that I am quite late, but I have just created a custom UIDatePicker subclass with a delegate:

This is the delegate:

// In TDDatePickerDelegate.h

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

@class TDDatePicker;
@protocol TDDatePickerDelegate

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

@end

And this is the subclass

// 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

Solution 3

Objective-C:

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

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

Swift:

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

func isRolled() {
    print("Date Picker has been rolled.")
}
Share:
16,152

Related videos on Youtube

oscarm
Author by

oscarm

Flex/Ruby-on-Rails/Objective-C/PHP Developer.

Updated on May 21, 2022

Comments

  • oscarm
    oscarm about 2 years

    I'm using a UIDatePicker in a modal view, to select a date. I found that if the view is closed while the date picker is still rolling, I got a EXC_BAD_ACCESS.

    How can I detect of the date picker is rolling when the user tab a button?

    I had two ideas:

    1) detect if the value has changed with this:

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

    But I would need to be able to detect if the value WILL change.

    2) check if the date is valid, but when the date picker is rolling, the previous date is returned, and of course is valid.

    Any ideas?

    • Dave DeLong
      Dave DeLong almost 13 years
      The date picker (and picker view) do not provide a way to know if one of the components is moving.
  • Aurum Aquila
    Aurum Aquila over 13 years
    UIDatePicker doesn't have a delegate.
  • Luis B
    Luis B about 10 years
    This is what I wanted a subclass. Thanks!