Mohammed Imran Mohammed Imran - 6 months ago 39
Objective-C Question

UIDatePickerModeTime limiting time selection from 9 AM to 5 PM

I have written following piece of code to bring a pickerView foe selecting time. which is working perfectly now i'm wondering how can I set a range of time between 9AM and 5 PM. Please can someone help me in achieving the requirement of limiting the time selection between 9 AM - 5 PM.

@interface CleaningDetails ()<UITextFieldDelegate>
{
IBOutlet TextFieldValidator *cleanTime;
UIDatePicker *time;
}


time = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
time.datePickerMode = UIDatePickerModeTime;


NSDate *minDate = [self beginningOfDay:[NSDate new]];
NSDate *maxDate = [self endOfDay:[NSDate new]];

[time setMinimumDate:minDate];
[time setMaximumDate:maxDate];

[self.cleanTime setInputView:time];
// add a toolbar with Cancel & Done button
UIToolbar *toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar1.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(showSelectedTime)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTouched:)];
// the middle button is to make the Done button align to right
[toolBar1 setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
[self.cleanTime setInputAccessoryView:toolBar1];
}


I have written the following to limit the time from 9 AM to 5 PM the methods are functioning only for the NSDate instance (the current date) How to pass the date selected by the user in the UIDatePicker.

-(NSDate *)beginningOfDay:(NSDate *)date1
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate:date1];

[components setHour:9];
[components setMinute:0];
[components setSecond:0];

return [cal dateFromComponents:components];

}

-(NSDate *)endOfDay:(NSDate *)date2
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond )fromDate:date2];

[components setHour:17];
[components setMinute:0];
[components setSecond:0];

return [cal dateFromComponents:components];

}

Answer
[urlRequest setHTTPBody:[noteDataString dataUsingEncoding:NSUTF8StringEncoding]];

this method can set the data you want to send to your server. your server received a string "http://domain/Customer_logon.php? email=XXXX&password=XXXX",the string is not a valid json, your php can not parse it.

so,if you need a json,you can write like this:

NSString *email = cusemail.text;
NSString *pass  = cuspassword.text;
NSDictionary *dic = @{@"email":email,@"password":pass};
[urlRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:dic options:0 error:nil]];
Comments