0

如果您需要输入城市名称,我有一个 UITextField。该城市名称链接到一个 url:例如 mywebsite/oslo

但是,当我进入纽约时,它不是一个正确的网址。mywebsite/new york 它必须是这个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];
}];

}

4

3 回答 3

0

用这个

 [searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
于 2013-10-21T10:44:07.050 回答
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:@"-"];
于 2013-10-21T11:00:02.630 回答
-1

尝试这个

NSString *string = @"Hi I am Indra          ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
                                  [NSCharacterSet whitespaceCharacterSet]];

从字符串替换空格

NSString *str = @"This is a string";

str = [str stringByReplacingOccurrencesOfString:@" "
                                     withString:@""];
于 2013-10-21T10:43:25.710 回答