如何在Web服务调用中使用%20而不在UITextField中显示它

时间:2014-06-14 19:04:32

标签: mysql ios string date

我正在尝试使用日期选择器中的选定日期并将其作为部分登录凭据的开始日期实施。此日期选择器集成在 UIAlert 作为键盘,它显示在主警报框的 UITextField 中。

-(void)requestStartTime

{

//create the alertview
startTimeAlert = [[UIAlertView alloc] initWithTitle:@"Sales Start:"
                                          message:nil
                                         delegate:self
                                cancelButtonTitle:@"Cancel"
                                otherButtonTitles:@"Next", nil];
startTimeAlert.alertViewStyle = UIAlertViewStylePlainTextInput;



//create the datepicker
startTimeDatePicker = [[UIDatePicker alloc] init];
[startTimeDatePicker addTarget:self action:@selector(startTimeDateChanged:) forControlEvents:UIControlEventValueChanged];



startTimeDatePicker.datePickerMode = UIDatePickerModeDateAndTime;
NSDate *now = [NSDate date];
startTimeDatePicker.maximumDate = now;


NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
[comps setHour:8];
[comps setMinute:0];
[comps setSecond:0];

NSDate *dateFromComps = [calendar dateFromComponents:comps]; // current date/month/year set to 8:00am


startTimeDatePicker.date = dateFromComps;
//get the textfield provided in the plain text alertview
startTimeTextField = [startTimeAlert textFieldAtIndex:0];
//change the textfields inputView to the date picker
startTimeTextField.inputView = startTimeDatePicker;
[startTimeTextField setTextAlignment:NSTextAlignmentCenter];
//create a date formatter to suitably show the date


startTimeDateFormatter = [[NSDateFormatter alloc] init];
[startTimeDateFormatter setDateFormat:@"yyyy-MM-dd%20HH:mm:ss"];
startTimeTextField.text = [startTimeDateFormatter stringFromDate:dateFromComps];

[startTimeAlert show];
[startTimeTextField becomeFirstResponder];

self.selectedStartDate = startTimeTextField.text;
}

如果您注意到,我使用的日期格式是 @“yyyy-MM-dd%20HH:mm:ss”。我必须使用它,因为它取代了 space 字符。

self.selectedStartDate = startTimeTextField.text;

selectedStartDate是我的字符串,startTimeTextField是我的日期选择的输入。 下面,我将使用此字符串将我的连接方法发布到MYSQL。

NSString *salesStr = @"http://";
salesStr = [salesStr stringByAppendingString:host];
salesStr = [salesStr stringByAppendingString:@":8080/sales.php?password="];
salesStr = [salesStr stringByAppendingString:pass];
salesStr = [salesStr stringByAppendingString:@"&db="];
salesStr = [salesStr stringByAppendingString:db];
salesStr = [salesStr stringByAppendingString:@"&sdate="];
salesStr = [salesStr stringByAppendingString:selectedStartDate];
salesStr = [salesStr stringByAppendingString:@"&edate="];
salesStr = [salesStr stringByAppendingString:selectedEndDate];

这一切都适合我。 我的问题是:如何使用%20而不在文本字段中显示?它看起来并不专业,我需要它来显示空格而不是%20并且仍能正常运行。

1 个答案:

答案 0 :(得分:1)

将日期格式设置为“yyyy-MM-dd HH:mm:ss”并将其显示在文本字段中,不显示百分比转义:

[startTimeDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

当您创建查询字符串时,请使用stringByAddingPercentEscapesUsingEncoding

salesStr = [salesStr stringByAppendingString:[selectedStartDate stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];