热更新demo

topicSeachController.m 10KB

    // // topicSeachController.m // ThePaperBase // // Created by scar1900 on 15/8/7. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "topicSeachController.h" #import "topicSearchListViewController.h" @interface topicSeachController() <topicSearchDelegate, UITextFieldDelegate> @property(nonatomic, strong)UIView *cantainerView; @property(nonatomic, strong)UIImageView *searchIcon; @property(nonatomic, strong)UITextField *searchContent; @property(nonatomic, strong)UIButton *searchBtn; @property(nonatomic, strong)topicSearchListViewController *tableviewVC; @property(nonatomic, strong)UILabel *noDataView; @end @implementation topicSeachController - (void)viewDidLoad { [super viewDidLoad]; [MobClick event:@"74"]; self.titleStr = @"问吧搜索"; self.view.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode) name:REFRESHAFTERNIGHTMODE object:nil]; [self.view addSubview:self.cantainerView]; [self.cantainerView addSubview:self.searchIcon]; [self.cantainerView addSubview:self.searchContent]; [self.view addSubview:self.searchBtn]; [self addChildViewController:self.tableviewVC]; [self.view addSubview:self.tableviewVC.view]; [self.tableviewVC didMoveToParentViewController:self]; [self.view addSubview:self.noDataView]; [self.cantainerView makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.naviBar.bottom).offset(5); make.left.equalTo(self.view.left).offset(10); make.height.equalTo(@30); }]; [self.searchIcon makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.cantainerView.left).offset(9); make.centerY.equalTo(self.cantainerView.centerY); make.width.mas_equalTo(@11); make.height.mas_equalTo(@12); }]; [self.searchContent makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.searchIcon.right).offset(5); make.right.equalTo(self.cantainerView.right).offset(-5); make.centerY.equalTo(self.cantainerView.centerY); make.height.equalTo(@28); }]; [self.searchBtn makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.cantainerView.top); make.left.equalTo(self.cantainerView.right); make.bottom.equalTo(self.cantainerView.bottom); make.right.equalTo(self.view.right); make.width.equalTo(@50); }]; [self.tableviewVC.view makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.cantainerView.bottom); make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); make.bottom.equalTo(self.view.bottom); }]; [self.noDataView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(self.barHeight, 0, 0, 0)); }]; self.noDataView.hidden = YES; self.tableviewVC.view.hidden = YES; [self.searchContent becomeFirstResponder]; //bug5070: 自动弹出键盘 } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)needrefreshNightMode { self.view.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; self.cantainerView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.cantainerView.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; self.searchContent.textColor = [UIColor colorWithHexString:TextBlack]; [self.searchBtn setTitleColor:[UIColor colorWithHexString:TextBlack] forState:UIControlStateNormal]; [self.searchBtn setTitleColor:[UIColor colorWithHexString:TextGray] forState:UIControlStateHighlighted]; self.noDataView.textColor = [UIColor colorWithHexString:TextGray]; } - (UIView*)cantainerView { if (!_cantainerView) { _cantainerView = [[UIView alloc] init]; _cantainerView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; _cantainerView.layer.borderWidth = 1; _cantainerView.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; _cantainerView.layer.cornerRadius = 4; } return _cantainerView; } - (UIImageView *)searchIcon { if (!_searchIcon) { _searchIcon = [[UIImageView alloc] init]; _searchIcon.image = Image(@"setting/search_icon.png"); } return _searchIcon; } - (UITextField *)searchContent { if (!_searchContent) { _searchContent = [[UITextField alloc] init]; _searchContent.textColor = [UIColor colorWithHexString:TextBlack]; _searchContent.textAlignment = NSTextAlignmentLeft; if ([[TPUserDefault instance].isNightMode intValue] > 0) _searchContent.keyboardAppearance = UIKeyboardAppearanceDark; else _searchContent.keyboardAppearance = UIKeyboardAppearanceDefault; _searchContent.keyboardType = UIKeyboardTypeDefault; _searchContent.backgroundColor = [UIColor clearColor]; _searchContent.clearButtonMode = UITextFieldViewModeWhileEditing; _searchContent.font = appFont(TEXT_FIVE_LEVELSIZE, NO); _searchContent.placeholder = @"话题搜索"; [_searchContent addTarget:self action:@selector(editChange:) forControlEvents:UIControlEventEditingChanged]; _searchContent.delegate = self; _searchContent.returnKeyType = UIReturnKeySearch; //bug:5036 键盘右下角改成“搜索” } return _searchContent; } - (UIButton *)searchBtn { if (!_searchBtn) { _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _searchBtn.titleLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO); [_searchBtn setTitleColor:[UIColor colorWithHexString:TextBlack] forState:UIControlStateNormal]; [_searchBtn setTitleColor:[UIColor colorWithHexString:TextGray] forState:UIControlStateHighlighted]; [_searchBtn setTitle:@"搜索" forState:UIControlStateNormal]; [_searchBtn addTarget:self action:@selector(searchBtnSelector) forControlEvents:UIControlEventTouchUpInside]; _searchBtn.backgroundColor = [UIColor clearColor]; } return _searchBtn; } - (TPTableViewController*)tableviewVC { if (!_tableviewVC) { _tableviewVC = topicSearchListViewController.new; _tableviewVC.delegate = self; } return _tableviewVC; } - (UILabel *)noDataView { if (!_noDataView) { _noDataView = [[UILabel alloc]init]; _noDataView.textAlignment = NSTextAlignmentCenter; _noDataView.font = appFont(TEXT_THREE_LEVELSIZE, NO); _noDataView.textColor = [UIColor colorWithHexString:TextGray]; _noDataView.text = @"暂无相关话题"; } return _noDataView; } #pragma mark - textfield event - (void)editChange:(UITextField*)textField { if (textField == self.searchContent) { if ([textField.text isMatchedByRegex:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"]) { textField.text = disable_emoji(textField.text); } if (textField.text.length > 20) { textField.text = [textField.text substringToIndex:20]; } } } #pragma mark - searchBtn event - (void)searchBtnSelector { if ([self.searchContent isFirstResponder]) { [self.searchContent resignFirstResponder]; } if (isBlankString(self.searchContent.text)) { ShowTextMessage(@"搜索关键词不能为空!"); return; } else { NSDictionary *dic = [NSDictionary dictionary]; NSString *content = self.searchContent.text; dic = @{@"k":content}; self.tableviewVC.searchStr = content; [self remoteAskForSearchList:dic]; } } - (void)remoteAskForSearchList:(NSDictionary*)dic { __weak typeof(self) Self = self; [Remote doJsonActionWithBlock:1 requestUrl:topicSearchURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { NSMutableArray *list = responseData[@"searchList"]; NSMutableArray *searchList = [NSMutableArray array]; [list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { TopicInfoBO *topic = setJsonDicToDataModel(obj, [TopicInfoBO class]); [searchList addObject:topic]; }]; if (searchList.count) { Self.tableviewVC.dataList = searchList; [Self.tableviewVC.tableView setContentOffset:CGPointZero]; NSString *url = responseData[@"nextUrl"]; Self.tableviewVC.nextUrl = url; Self.tableviewVC.view.hidden = NO; Self.noDataView.hidden = YES; } else { Self.tableviewVC.view.hidden = YES; Self.noDataView.hidden = NO; } }else { Self.tableviewVC.view.hidden = YES; Self.noDataView.hidden = YES; ShowTextMessage(message); } }]; } #pragma mark - topicSearchListViewController delegate - (void)searchGoToContent:(TopicInfoBO*)data { pushTopicPageWithTopicInfo(self.navigationController, data); } #pragma mark - textfield delegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { if ([self.searchContent isFirstResponder]) { [self.searchContent resignFirstResponder]; } [self searchBtnSelector]; return YES; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([textField isFirstResponder]) { if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) { return NO; } } return YES; } #pragma mark - keyboard - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if ([self.searchContent isFirstResponder]) { [self.searchContent resignFirstResponder]; } } #pragma mark - navibar - (void)tapNaviBar:(id)sender { if (![self.tableviewVC.view isHidden]) { [self.tableviewVC.tableView setContentOffset:CGPointZero animated:YES]; return; } } @end