我对 iOS 开发非常陌生。我需要UITextField
点击一下添加日历。如何添加自定义日历,因为我不想添加UIDatePicker
.
3 回答
首先以多种方式实现日历。但是大多数通过TapkuLibrary http://github.com/devinross/tapkulibrary填充和轻松获取日历,然后在您的类文件中实现这个TapkuLibrary 。在为显示日历实现以下代码后
头文件.h
#import <UIKit/UIKit.h>
#import "Libraries/TapkuLibrary/TKCalendarMonthView.h"
@interface calendarViewController : UIViewController<TKCalendarMonthViewDelegate,TKCalendarMonthViewDataSource, UITextFieldDelegate>
{
TKCalendarMonthView *calendar;
UITextField *txtField;
}
@property(nonatomic,retain)UITextField *txtField;
@结尾
实现文件.h
// @synthesize.......
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self doAddAction];
return YES;
}
-(void)doAddAction
{
calendar=[[TKCalendarMonthView alloc] init];
calendar.frame=CGRectMake(0,0,calendar.frame.size.width,calendar.frame.size.height);
calendar.delegate=self;
calendar.dataSource=self;
[self.view addSubview:calendar];
}
#pragma mark -
#pragma mark TKCalendarMonthViewDelegate methods
- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d
{
NSDateFormatter *objDateFormatter = [[NSDateFormatter alloc] init];
[objDateFormatter setDateFormat:@"dd-MM-yyyy"];
NSLog(@"%@",[objDateFormatter stringFromDate:d]);
}
- (void)calendarMonthView:(TKCalendarMonthView *)monthView monthDidChange:(NSDate *)d {
NSLog(@"calendarMonthView monthDidChange");
}
#pragma mark -
#pragma mark TKCalendarMonthViewDataSource methods
- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate {
NSLog(@"calendarMonthView marksFromDate toDate");
NSLog(@"Make sure to update 'data' variable to pull from CoreData, website, User Defaults, or some other source.");
// When testing initially you will have to update the dates in this array so they are visible at the
// time frame you are testing the code.
NSArray *data = [NSArray arrayWithObjects:
@"2011-01-01 00:00:00 +0000", @"2011-01-09 00:00:00 +0000", @"2011-01-22 00:00:00 +0000",
@"2011-01-10 00:00:00 +0000", @"2011-01-11 00:00:00 +0000", @"2011-01-12 00:00:00 +0000",
@"2011-01-15 00:00:00 +0000", @"2011-01-28 00:00:00 +0000", @"2011-01-04 00:00:00 +0000",
@"2011-01-16 00:00:00 +0000", @"2011-01-18 00:00:00 +0000", @"2011-01-19 00:00:00 +0000",
@"2011-01-23 00:00:00 +0000", @"2011-01-24 00:00:00 +0000", @"2011-01-25 00:00:00 +0000",
@"2011-02-01 00:00:00 +0000", @"2011-03-01 00:00:00 +0000", @"2011-04-01 00:00:00 +0000",
@"2011-05-01 00:00:00 +0000", @"2011-06-01 00:00:00 +0000", @"2011-07-01 00:00:00 +0000",
@"2011-08-01 00:00:00 +0000", @"2011-09-01 00:00:00 +0000", @"2011-10-01 00:00:00 +0000",
@"2011-11-01 00:00:00 +0000", @"2011-12-01 00:00:00 +0000", nil];
// Initialise empty marks array, this will be populated with TRUE/FALSE in order for each day a marker should be placed on.
NSMutableArray *marks = [NSMutableArray array];
// Initialise calendar to current type and set the timezone to never have daylight saving
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Construct DateComponents based on startDate so the iterating date can be created.
// Its massively important to do this assigning via the NSCalendar and NSDateComponents because of daylight saving has been removed
// with the timezone that was set above. If you just used "startDate" directly (ie, NSDate *date = startDate;) as the first
// iterating date then times would go up and down based on daylight savings.
NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit |
NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit)
fromDate:startDate];
NSDate *d = [cal dateFromComponents:comp];
// Init offset components to increment days in the loop by one each time
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
// for each date between start date and end date check if they exist in the data array
while (YES) {
// Is the date beyond the last date? If so, exit the loop.
// NSOrderedDescending = the left value is greater than the right
if ([d compare:lastDate] == NSOrderedDescending) {
break;
}
// If the date is in the data array, add it to the marks array, else don't
if ([data containsObject:[d description]]) {
[marks addObject:[NSNumber numberWithBool:YES]];
} else {
[marks addObject:[NSNumber numberWithBool:NO]];
}
// Increment day using offset components (ie, 1 day in this instance)
d = [cal dateByAddingComponents:offsetComponents toDate:d options:0];
}
[offsetComponents release];
return [NSArray arrayWithArray:marks];
}
没有内置的日历视图控制器有点令人震惊,但实际上没有。我真的希望他们在 iOS4 中使用 EventKit 添加一个,但没有。Kal 有点难以理解,但是一旦你理解了它的想法,它就很容易实现。
在此链接上查看日历的整个库...
http://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=日历
并且还看到这个tapku库它也是非常有用的库..
http://maniacdev.com/2011/09/tutorial-using-the-tapku-library-to-create-an-elegant-marked-calendar/
你也可以触发事件textFieldDidBeginEditing:
:)
我想你想添加一个日历,但不是通过日期选择器,对吗??所以这里有一些链接......请检查它们,我认为它们会解决您的问题。
http://www.cocoacontrols.com/platforms/ios/controls/occalendar
http://www.cocoacontrols.com/platforms/ios/controls/pmcalendar
http://www.cocoacontrols.com/platforms/ios/controls/ckcalendar
它们包括各种类型的定制日历..任何问题请告诉我:)