2013-07-25 1 views
0

Obj-C 학습을 시작했습니다. 있다면 순진한 질문들을 변명하십시오.datepicker에서 선택한 날짜를 기준으로 한 사용자 지정 알림보기

datepicker에서 선택한 날짜를 기반으로 사용자 지정 경고보기를 보여주는 응용 프로그램을 만들려고합니다.

이것은 날짜가 선택되고 단추가 누를 때 하드 코딩 된 경고보기를 보여주는 코드입니다. 내가 어떻게 선택한 날짜에 의존하게 만들 수 있습니다.

(#)import "APViewController.h" 
@interface APViewController() 
@end 

@implementation APViewController 
@synthesize datePicker; 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

- (IBAction)specialButton:(id)sender { 

// NSDate *chosen = [datePicker date]; 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Woohoo !" message:@"Its your 50th Birthday" delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil]; 

[alert show]; 

} 

@end  
+0

"선택한 날짜에 의존"이란 무엇을 의미합니까? 그 날짜가 경보에 나타나기를 원합니까? – rdelmar

+0

@rdelmar 선택기에서 어떤 날짜가 선택되었는지에 따라 alertview는 경고를 제공해야합니다. 7 월 2 일을 선택하면 8 월 10 일에 생일 축하합니다. 및 생일 축하 해요. 경고는 버튼을 누르면 실행됩니다. – aceprogramer

답변

0

당신이 그것을 원하는 방법 날짜 선택 날짜를 포맷하는 NSDateFormatter를 사용하여, 다음 경고에 그 표시 :

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"cccc, MMM d, hh:mm aa"]; 
NSString * dateString = [dateFormatter stringFromDate:datePicker.date]; 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Woohoo !" message:dateString delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil]; 

[alert show]; 

을 당신은 가정 사용자의 나이를 표시하려는 경우 날짜 선택기를 자신의 생일에 넣고 NSDateComponents를 사용하여 날짜의 연도와 현재 연도를 가져옵니다.

NSDateComponents * currentDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:[NSDate date]]; 
NSDateComponents * pickedDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:datePicker.date]; 

NSInteger diff = currentDateComponents.year - pickedDateComponents.year; 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Woohoo !" message:[NSString stringWithFormat:@"Its your %dth Birthday", diff] delegate:nil cancelButtonTitle:@"Thanks" otherButtonTitles:nil]; 

[alert show]; 

항상 "th"가 아닌지 확인해야합니다. 그것은 "st"또는 "nd"또는 "rd"일 수 있습니다.

0

데이터 구조가 필요합니다. 키가 날짜 인 사전이 바람직하며 값은 경고에 표시 할 문자열입니다. 날짜 선택 도구에서 날짜를 선택하면 해당 날짜와 일치하는 사전에서 키를 검색하고 해당 키 값을 경고보기의 메시지에 할당합니다.

+0

도와 주셔서 감사합니다. 몇 가지 샘플 코드를 제공해 주시겠습니까? 어떻게 할 수 있습니까? "날짜 선택 도구에서 날짜를 선택하면 해당 날짜와 일치하는 사전에서 키를 검색하십시오" – aceprogramer

+0

@aceprogramer, 자기가 먼저 그것을 해결하려고하면 더 잘 배우고, 네가 일할 수 없다면 구체적인 질문을한다. NSDictionary 메서드 인 allKeys를 사용하여 반복 할 수있는 키 배열을 가져와 선택한 날짜와 동등한 지 테스트 할 수 있습니다. 시도 해봐. – rdelmar

+0

나는 같은 줄에서 작업을하고 있지만, 선택한 모든 날짜에 값을 첨부 할 수는 없습니다. 이 작업을 수행했지만 선택한 날짜에 키를 연결하는 방법을 혼동했습니다. '- (void) viewDidLoad { [super viewDidLoad]; events = [[NSArray alloc] initWithObjects : @ "훌륭함", @ "훌륭함", @ "괜찮은", @ "살아 남음", @ "나쁜", nil]; dates = [[NSDictionary alloc] init]; date_keys = [dates allKeys]; } – aceprogramer

관련 문제