日历API更改

时间:2011-07-25 12:04:03

标签: iphone

http://code.google.com/p/scm-subversion/source/browse/trunk/iPhone/CalendarTest/?r=4#CalendarTest%253Fstate%253Dclosed

我正在使用上面提到的Calendar API。在CalendarTestViewController.m类中,我们得到了日期。现在我已经声明了一个全局变量nsstring类型,我想通过使用全局变量将该日期用于另一个新类。我已经尝试了很多次但无法获得输出。如果有人知道我如何使用Calendar API来使用所选日期,那么请给我一些解决方案。

提前致谢。

CODE

- (void)calendarView:(KLCalendarView *)calendarView tappedTile:(KLTile *)aTile{ 
NSLog(@"Date Selected is %@",[aTile date]); 
str=(NSString *)[aTile date];
} 
NSLog(@"str:%@",str); 
glbdate1 = (NSString *)[aTile date]; 
NSLog(@"glbdate1:%@",glbdate1);
} 
//I have declared the glbdate1 variable globally in app delegate file and i have made the new class calenderview 
//I want to display the date in textfield by using global variable. Here is code in calenderview

-(void)viewWillAppear:(BOOL)animated {
 if ([glbdate1 length] != 0) 
   { 
    from.text = glbdate1;
   } 
}

2 个答案:

答案 0 :(得分:1)

您必须按如下所示将NSDate转换为NSString,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-mm-dd HH:mm:ss"];
NSString *dateString=[dateFormatter stringFromDate:date];

然后可以将相同的NSString值转换为NSDate as,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-mm-dd HH:mm:ss"];
NSDate *dateFromString=[dateFormatter dateFromString:dateString];

答案 1 :(得分:1)

 -(BOOL)isDateinEventArray:(NSString*)iDateString{

//NSString *searchText = lblDate.text;


NSMutableArray *searchArray = [[NSMutableArray alloc] init];

for (ToDo *todoObj in appDelegate.todoArray)
{
        NSString *date = todoObj.startDate;
        [searchArray addObject:date]; 
}

if ([searchArray count] > 0) {

    for (NSString *sTemp in searchArray)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:iDateString 
                     options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)   
        [copyListOfItems addObject:sTemp];    
    }

    [searchArray release];
    searchArray = nil;

}

if ([copyListOfItems count] > 0) {  
    return TRUE;
}else{
    return FALSE;
}   
  }


   /*----- Calendar Delegates -----> */

  - (void)calendarView:(KLCalendarView *)calendarView tappedTile:(KLTile *)aTile{

NSLog(@"Date Selected is %@",[aTile date]);

//  NSString *stringFromDate = 
// [[NSString alloc]initWithString:[NSString stringWithFormat:@"%02i-%02i-%i",[aTile.date dayOfMonth],
//  

     [aTile.date monthOfYear],[aTile.date yearOfCommonEra]]];

NSString *stringFromDate = [[NSString alloc]initWithString:[NSString stringWithFormat:@"%02i-%02i-%02i",[aTile.date yearOfCommonEra],
                                                [aTile.date monthOfYear],[aTile.date dayOfMonth]]];


if([self isDateinEventArray:stringFromDate]){

    selectedDate = [aTile date];

    eventFoundMsg = @"Events are found on this date.";
    eventMsgTag = 1;
}else{
    eventFoundMsg = @"No events are found on this date.";
    eventMsgTag = 0;
}   
[stringFromDate release];

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Event Message" 

message:eventFoundMsg   
delegate:self
    cancelButtonTitle:@"Ok" 
    otherButtonTitles:nil]autorelease];
    alert.tag = eventMsgTag;
[alert show];
[aTile flash];
/*

if(tile == nil)
    tile = aTile;
else
    [tile restoreBackgroundColor];

 */


 }

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (eventMsgTag == 1) {

    if (buttonIndex == 0)
    {
        MyToDoView *detailViewController = [[MyToDoView alloc] 
                     initWithNibName:@"MyToDoView" bundle:nil];
        detailViewController.searchDate = selectedDate;
        NSLog([NSString stringWithFormat:@"%@",detailViewController.searchDate]);
        [detailViewController searchDateTableView];
        [self.navigationController popViewControllerAnimated:YES];
        [detailViewController release];
    }

}
}

我希望这可以帮助你......