2012-03-23 11 views
0

私は1つのdatepickerを持っています。私は、ユーザに "Von"テキストフィールド(明日午前9時に開始する)とその後にテキストフィールド "bis"(1.5時間後に始まる)の間で日付と時刻を選択させたいと思っています。2つのテキストフィールドを持つUIDatePicker(from/until date)は、 "until"テキストフィールドで正しい日付を設定できません。

すべて正常に動作します。しかし、私は "von"テキストフィールドをクリックします。私のdatepicker dintは自分自身に正しい時間を設定しました。ここで

は、いくつかのスクリーンショットは以下のとおりです。 from textfielduntil textfield

のviewDidLoad:ここ

NSDate *today = [NSDate date]; 
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    [gregorian setLocale:[NSLocale currentLocale]]; 

    NSDateComponents *nowComponents = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit| NSWeekdayCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today]; 


    [nowComponents setWeek: [nowComponents week]]; 
    [nowComponents setDay:[nowComponents day]+1]; 
    [nowComponents setHour:9]; 
    [nowComponents setMinute:0]; 
    [nowComponents setSecond:0]; 

    today = [gregorian dateFromComponents:nowComponents]; 

    NSDate *tomorrowat9am = [gregorian dateFromComponents:nowComponents]; 
    //DatePicker wird erstellt 

    self.thePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 152, 325, 300)]; 
    self.thePicker.datePickerMode = UIDatePickerModeDateAndTime; 
    [thePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; 



    //starts from tomorrow at 9 am 
    NSDate *pickereinstelldate= tomorrowat9am; 

    [self.thePicker setDate:pickereinstelldate]; 

    //set the right format 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; 
    NSString *datum =[dateFormatter stringFromDate:self.thePicker.date]; 


//set the date at textfield "bis" to 1.5 hours later 

    NSDate *neuesdatum = [beginningOfWeek dateByAddingTimeInterval:3600*1.5];   NSString *neuesd = [dateFormatter stringFromDate:neuesdatum]; 


    //set the textfield with the new date 
    tfVon.text=datum; 
    tfBis.text=neuesd; 

    [self.view addSubview:thePicker]; 
    self.thePicker.hidden=YES; 
    tfVon.inputView=thePicker; 
    tfBis.inputView=thePicker; 
} 

はアクションです:私は何に私のdateboolを設定していないのはここ

-(IBAction)dateChanged 
{ 
    self.date = [thePicker date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; 
    NSString *datum =[dateFormatter stringFromDate:self.date]; 
    NSDate *neuesdatum = [date dateByAddingTimeInterval:3600*1.5]; 
    NSString *neuesd = [dateFormatter stringFromDate:neuesdatum]; 

    //when i click on "Von" textfield 
    if (dateBOOL==YES) 

    { 
     tfVon.text=datum; 

     tfBis.text=neuesd; 

    } 
    //when i click on "bis" textfield 
    if (dateBOOL==NO) { 
     tfBis.text=neuesd; 

     }  

} 

は次のとおりです。

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 


    if (textField==tfVon) 
    { 

     dateBOOL=YES; 
     self.thePicker.hidden=NO; 

     return YES; 
    } 
    if (textField==tfBis) 
    { 

     dateBOOL=NO; 
     self.thePicker.hidden=NO; 
     return YES; 
    } 

    } 
return YES; 

} 
+1

不明な質問があります。 – Hiren

+0

いつ日付ピッカーを表示しますか?そして 'dateBOOL'をどこに' 'いいえ 'に設定しますか?いくつかのコードを投稿できますか? – sch

+0

textxフィールドが選択されたときに私のdatepickerを表示します(自分のコードを更新しました)。日付帳はtextfieldにある必要があります。 –

答えて

0

あなたが投稿した最後の方法は、最後の返品にはなりません。あなたは余分な}を持っています。

"私の日付ピッカーの味が自分自身に正しい時間を設定しました。" あなたの質問は完全にはっきりしませんが、私が収集したものから、「bis」テキストフィールドを選択した後、日付選択ツールを正しい時間に設定しようとしています。

[datePicker setDate:neuesdatum animated:NO];

bisテキストフィールドを新しい日付に設定しようとする場合は、再度dateformatterを使用して、その新しい日付にテキストフィールドを設定する必要があります。

現在あなたに与えている次のライン(neuesd)はどのような値ですか? NSString * neuesd = [dateFormatter stringFromDate:neuesdatum];

+0

重要ではないコードを削除しました。}記号を削除するのを忘れました。 nsstring * neuesdはdatepickerから値を取り出し、それを1.5時間後に設定します。 "bis"のテキストフィールドをクリックすると、datepickerの日付をneuesdに設定します。私は1.5時間後に始めることができます –

関連する問題