Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.67 MB, 89 trang )
Figure 2-14. A date picker shown at the bottom of the screen
Let's get started by first declaring a property of type UIDatePicker and then allocating
and initializing this property and adding it to the view of our view controller:
#import
@interface Picking_Date_and_Time_with_UIDatePickerViewController
: UIViewController
@property (nonatomic, strong) UIDatePicker *myDatePicker;
@end
Let's synthesize the property now:
#import "Picking_Date_and_Time_with_UIDatePickerViewController.h"
@implementation Picking_Date_and_Time_with_UIDatePickerViewController
@synthesize myDatePicker;
2.4 Picking Date and Time with UIDatePicker | 137
...
And now let's instantiate the date picker, as planned:
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.myDatePicker = [[UIDatePicker alloc] init];
self.myDatePicker.center = self.view.center;
[self.view addSubview:self.myDatePicker];
}
Now let's run the app and see how it looks like:
138 | Chapter 2: Implementing Controllers and Views
Figure 2-15. A simple date picker
You can see that the date picker, by default, has picked today's date. The first thing
that we need to know about date pickers is that they can have different styles or modes.
This mode can be changed through the datePickerMode property which is of type UIDa
tePickerMode:
typedef enum {
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer,
} UIDatePickerMode;
2.4 Picking Date and Time with UIDatePicker | 139
Depending on what you need, you can set the mode of your date picker to any of the
values listed in the UIDatePickerMode enumeration.
Now that you have successfully displayed a date picker on the screen, you can attempt
to retrieve its currently-selected date using its date property or alternatively, you can
call the date method on the date picker, like so:
NSDate *currentDate = self.myDatePicker.date;
NSLog(@"Date = %@", currentDate);
Just like the UISwitch class, a date picker also sends action messages to its targets
whenever the selection of date in it has changed. To respond to these messages, the
receiver must add itself as the target of the date picker, using the addTarget:action:for
ControlEvents: method, like so:
- (void) datePickerDateChanged:(UIDatePicker *)paramDatePicker{
if ([paramDatePicker isEqual:self.myDatePicker]){
NSLog(@"Selected date = %@", paramDatePicker.date);
}
}
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.myDatePicker = [[UIDatePicker alloc] init];
self.myDatePicker.center = self.view.center;
[self.view addSubview:self.myDatePicker];
[self.myDatePicker addTarget:self
action:@selector(datePickerDateChanged:)
forControlEvents:UIControlEventValueChanged];
}
Now ever time the date changes, you will get a message from the date picker.
With date picker, you also have the ability to set the minimum and the maximum dates
that can be displayed in a date picker. For this, let's first switch our date picker mode
to UIDatePickerModeDate and then using the maximumDate and the minimumDate properties, adjust this range:
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.myDatePicker = [[UIDatePicker alloc] init];
self.myDatePicker.center = self.view.center;
self.myDatePicker.datePickerMode = UIDatePickerModeDate;
[self.view addSubview:self.myDatePicker];
NSTimeInterval oneYearTime = 365 * 24 * 60 * 60;
NSDate *todayDate = [NSDate date];
140 | Chapter 2: Implementing Controllers and Views
NSDate *oneYearFromToday = [todayDate
dateByAddingTimeInterval:oneYearTime];
NSDate *twoYearsFromToday = [todayDate
dateByAddingTimeInterval:2 * oneYearTime];
self.myDatePicker.minimumDate = oneYearFromToday;
self.myDatePicker.maximumDate = twoYearsFromToday;
}
So with these two properties, we can then limit the user's selection on the date to a
specific range, as shown in Figure 2-16. In this example code, we have limited the user's
input of the dates to the range of a year to two years from now. So suppose the current
date is 01/Jan/2012. With this code, the user can only select a date between 01/Jan/
2013 and 01/Jan/2014.
2.4 Picking Date and Time with UIDatePicker | 141
Figure 2-16. Minimum and maximum dates applied to a date picker
If you want to use the date picker as a countdown timer, then you must set your date
picker mode to UIDatePickerModeCountDownTimer and use the countDownDuration property of the date picker to specify the default countdown duration. For instance, if you
want to present a countdown picker to the user and set the default countdown duration
to 2 minuts, you would write your code like this:
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.myDatePicker = [[UIDatePicker alloc] init];
142 | Chapter 2: Implementing Controllers and Views
self.myDatePicker.center = self.view.center;
self.myDatePicker.datePickerMode = UIDatePickerModeCountDownTimer;
[self.view addSubview:self.myDatePicker];
}
NSTimeInterval twoMinutes = 2 * 60;
[self.myDatePicker setCountDownDuration:twoMinutes];
And the results are shown in Figure 2-17:
Figure 2-17. A two minute countdown duration set on a date picker
2.4 Picking Date and Time with UIDatePicker | 143
See Also
XXX
2.5 Implementing Range Pickers with UISlider
Problem
You would like to allow your users to specify a value within a range, using an easy-touse and intuitive UI.
Solution
Use the UISlider class.
Discussion
You've certainly seen slideres before. Here is an example:
144 | Chapter 2: Implementing Controllers and Views
Figure 2-18. The volume slider at the bottom of the screen
We instantiate objects of type UISlider to create sliders. Let's dive right in and create
a slider and place it on our view controller's view. Let's start with our view controller's
header file:
#import
@interface Implementing_Range_Pickers_with_UISliderViewController
: UIViewController
@property (nonatomic, strong) UISlider *mySlider;
@end
And then let's synthesize the mySlider property:
#import "Implementing_Range_Pickers_with_UISliderViewController.h"
@implementation Implementing_Range_Pickers_with_UISliderViewController
2.5 Implementing Range Pickers with UISlider | 145