Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if UIDatePicker is rolling?

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?

like image 393
oscarm Avatar asked Feb 20 '11 06:02

oscarm


3 Answers

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];
}
like image 87
Bogatyr Avatar answered Nov 20 '22 19:11

Bogatyr


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
like image 4
Garoal Avatar answered Nov 20 '22 18:11

Garoal


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.")
}
like image 1
fujianjin6471 Avatar answered Nov 20 '22 18:11

fujianjin6471