开发者社区> 问答> 正文

点击UISearchBar 呼唤不出键盘

在做自己ios天气应用的时候,用到UISearchBar,可是不知道为什么点击后没有键盘出现,麻烦看看我的代码是不是有什么问题。代码如下

#import "SketchWeatherViewController.h"
#import "OWMWeatherAPI.h"

@interface SketchWeatherViewController () <UISearchBarDelegate>
{
    OWMWeatherAPI *_weatherAPI;
    NSArray *_forecast;
    NSDateFormatter *_dateformatter;
    int downloadCount;
    int count;
}

@end

@implementation SketchWeatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _forecast = @[];

    _weatherAPI = [[OWMWeatherAPI alloc] initWithAPIKey:@"53bcc818743498ea24bebe70cf2cba0a"];
    [_weatherAPI setLangWithPreferedLanguage];
    [_weatherAPI setTemperatureFormat:kOWMTempCelcius];

    self.searchbar.delegate = self;
    self.searchbar.placeholder = [NSString stringWithFormat:@"搜索你的城市"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UISearchBarDelegate

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchbar{

    [self.activityIndicator startAnimating];

    [_weatherAPI currentWeatherByCityName:self.searchbar.text withCallback:^(NSError *error, NSDictionary *result) {

        downloadCount++;

        if (downloadCount > 1) [self.activityIndicator stopAnimating];

        if (error) {
            // Handle the error
            return;
        }

        self.sunIcon.image = [UIImage imageNamed:@"01d"];
        self.moonIcon.image = [UIImage imageNamed:@"01n"];
        self.weatherIcon.image = [UIImage imageNamed:result[@"weather"][@"Icon"]];
        self.cityName.text = [NSString stringWithFormat:@"%@, %@", result[@"name"],result[@"sys"][@"country"]];
        self.cityWeather.text = [NSString stringWithFormat:@"%d℃,%@", (int)result[@"main"][@"temp"],result[@"weather"][0][@"description"]];
        self.sunriseTime.text = [_dateformatter stringFromDate:result[@"sys"][@"sunrise"]];
        self.sunsetTime.text = [_dateformatter stringFromDate:result[@"sys"][@"sunset"]];
    }];


    [_weatherAPI dailyForecastWeatherByCityName:self.searchbar.text withCount:count andCallback:^(NSError *error, NSDictionary *result){

        downloadCount++;

        if (downloadCount > 1) [self.activityIndicator stopAnimating];

        if (error) {
            // Handle the error
            return;
        }
        _forecast = result[@"list"];
        [self.tableview reloadData];
    }];
}

展开
收起
a123456678 2016-07-28 10:03:45 1995 0
1 条回答
写回答
取消 提交回答
  • 模拟器上commond+K

    2019-07-17 20:01:02
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载