用按钮编辑UIDatePicker?

时间:2010-09-24 17:26:04

标签: iphone objective-c nsdate uidatepicker

在我的应用程序中,我有下一周和上周按钮,但也有UIDatePicker。那么让我说按净周按钮,如何调整UIDatePicker中的日期增加7天?反过来7天也是如此。

谢谢!

1 个答案:

答案 0 :(得分:1)

使用dateByAddingTimeInterval:类的NSDate方法和setDate:animated:类的UIDatePicker方法来调整选择器的当前日期:

NSDate *currentDate = [datePicker date];

// Set the date 7 days earlier
NSDate *sevenDaysEarlier = [currentDate dateByAddingTimeInterval:-(60 * 60 * 24 * 7)];
[datePicker setDate:sevenDaysEarlier animated:YES];

// Set the date 7 days later
NSDate *sevenDaysLater = [currentDate dateByAddingTimeInterval:60 * 60 * 24 * 7];
[datePicker setDate:sevenDaysLater animated:YES];