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 t开发者_高级运维he 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?
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];
}
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
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.")
}
Officially, you can't retrieve the state of the picker in these terms with just Apple's official UIDatePicker
methods.
If there are solutions out there, however, I'd be curious to see them.
There is a trick to detect this but there is no delegate method/ property to detect if its scrolling or not
- take a property as isScrolling
- set isScrolling to true in
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
or equivalent method - set isScrolling to true in
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
- enable the close button for modal view only if picker has reached to a finite state that is scrolling is disabled
hope this helps
精彩评论