2011-07-25 31 views
0

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

我正在使用上面提到的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; 
    } 
} 
+0

添加笏ü甲肝试过的一些代码.. – KingofBliss

+0

- (空)calendarView:(KLCalendarView *)calendarView tappedTile:(KLTile *)aTile { \t的NSLog(@ “选择的日期是%@”,[aTile date]); (@“str:%@”,str);} \t str =(NSString *)[aTile date];} \t NSLog \t \t glbdate1 =(NSString *)[aTile date];我已经在应用程序委托文件中全局声明了glbdate1变量,并且我已经创建了新的类calenderview我想通过使用全局变量在文本字段中显示日期。这里是calenderview中的代码 - (void)viewWillAppear:(BOOL)动画 if([glbdate1 length]!= 0){ \t from.text = glbdate1; } –

回答

1

你必须给的NSDate转换为NSString的如下,

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

那么同样的NSString值可以被转换成NSDate的作为,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-mm-dd HH:mm:ss"]; 
NSDate *dateFromString=[dateFormatter dateFromString:dateString]; 
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]; 
    } 

} 
} 

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