我有一个UITextField你需要输入城市名称。该城市名称链接到网址:例如mywebsite / oslo
但是,当我进入纽约时,它不是一个正确的网址。 mywebsite /纽约 它必须是这个http://www.website.com/New-York 如何创建一个用以下内容替换空格的UITextField: - > -
感谢您的帮助!
- (void)configureControls {
[self.activityIndicator stopAnimating];
[UIView animateWithDuration:1.5 animations:^{
if (self.weatherModel.lastLoadFailed) {
self.locationTextField.enabled = YES;
self.saveButton.hidden = NO;
self.activityIndicator.hidden = YES;
return;
}
self.settingsModel.location = self.locationTextField.text;
PWForecastModel *forecast = self.weatherModel.sevenDayForecast[0];
NSString *backgroundName = [PWHelper backgroundFileNameForConditionCode:forecast.conditions.conditionCode asDaytime:[[NSDate date] isDayTime]];
if (![[NSDate date] isDayTime]) {
[self styleControlsForDarkBackground];
}
self.backgroundImageView.image = [UIImage imageNamed:backgroundName];
self.locationLabel.text = [NSString stringWithFormat:@"%@",self.weatherModel.cityName];
}];
}
答案 0 :(得分:0)
使用此
[searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
答案 1 :(得分:0)
您需要调用委托方法textFieldShouldEndEditing
。你只需使用这个stringByReplacingOccurrencesOfString
。或者也可以在任何事件中使用它。就这样 。希望这会有所帮助。
参考:
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
// NSString *str= [textField.text stringByReplacingOccurrencesOfString:@" " withString:@"-"];
// NSLog(@"Testing: %@",str);
textField.text = [NSString stringWithFormat:@"www.myweb.com/%@",[textField.text stringByReplacingOccurrencesOfString:@" " withString:@"-"]];
return YES;
}
我想,你只需要这个:
[self.weatherModel.cityName stringByReplacingOccurrencesOfString:@" " withString:@"-"];
答案 2 :(得分:-1)
试试这个
NSString *string = @"Hi I am Indra ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
替换字符串
中的空格NSString *str = @"This is a string";
str = [str stringByReplacingOccurrencesOfString:@" "
withString:@""];