您的位置:首页 > 其它

最新关于TextField的不能够即时拿到文本框输入值的问题解决

2016-07-08 11:03 567 查看
使用到TextField代理方法去做拿到文本框里面的值然后去搜索相对应的结果做搜索款问题的时候

如果食用代理方法去得到输入框里面的值的话反应辉慢一步坑爹的代理方法,不知道那这个有什么用,

我这里的解决方法已经有了我们我整个项目类的代码复制到下面去

下面内容中大子体就是解决的问题;

#import "HBHomeViewController.h"

#import "TableViewCell.h"

#import "AppDelegate.h"

#import "ShowViewController.h"

#import <MJRefresh.h>

#import <AFNetworking.h>

#import <MJExtension.h>

#import "ModelHome.h"

#import <SVProgressHUD.h>

@interface HBHomeViewController () <UITableViewDataSource, UITableViewDelegate,TermCellDelegate> //!< 遵守tableView的两个协议

{

    NSInteger _clickIndex;

}

@property(nonatomic,strong)UILabel *lesseelabel;

@property(nonatomic,strong)UILabel *dealers;

@property(nonatomic,strong)UILabel *number;

@property(nonatomic,strong)UILabel *legalperson;

@property(nonatomic,strong)UITextField *lesseelTextField;

@property(nonatomic,strong)NSString *lesseelstring;

@property(nonatomic,strong)UITextField *dealersTextField;

@property(nonatomic,strong)NSString *dealersstring;

@property(nonatomic,strong)UITextField *numberTextField;

@property(nonatomic,strong)NSString *numberstring;

@property(nonatomic,strong)UISwitch *mySwitch;

@property(nonatomic,strong)UIButton *addbutton;

@property(nonatomic,strong)UIButton *querybutton;

@property(nonatomic,strong)UIView *topView;

@property(nonatomic,strong)UIView *secView;

@property(nonatomic,strong)UILabel *changeLabel;

@property(nonatomic,strong)NSMutableArray *categories;

@property(nonatomic,strong)NSMutableArray *categories2;

@property(nonatomic,strong)NSMutableArray *detailedData;

@property(nonatomic,assign)NSInteger currentPage;

@property(nonatomic,strong)NSMutableDictionary *params;

@property(nonatomic,strong)AFHTTPSessionManager *manager;

@property(nonatomic,strong)UITableView *tableView; //!< 表格视图属性;

@property(nonatomic,strong)NSMutableArray *dataSource; //!< 数据源;  存储数据源, 存储模型;

//@property(nonatomic,strong)UITableView *tableView2;//~>查找展示tableView;

@end

static NSString *const TableViewCellID = @"TableViewCell";

@implementation HBHomeViewController

#pragma mark - 传值使用

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    UIApplication *app = [UIApplication sharedApplication];

    AppDelegate *delegate = app.delegate;

    if (delegate.string != nil) {

        self.string = delegate.string;

        NSLog(@"self.string = %@",self.string);

    }

}

-(NSMutableArray *)_detailed{

    if (!_detailedData) {

        _detailedData = [NSMutableArray array];

    }

    return _detailedData;

}

-(NSMutableArray *)_categories{

    if (!_categories) {

        _categories = [NSMutableArray array];

    }

    return _categories;

}

- (AFHTTPSessionManager *)manager

{

    if (!_manager) {

        _manager = [AFHTTPSessionManager manager];

    }

    return _manager;

}

- (void)viewDidLoad {

    

    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle.png"]];

    [super viewDidLoad];

    [self creatView1];//~>创建第一个View

    [self creatView2];//~>创建第二个view

    [self creatField];//~>创建textField

    [self setupTableView];//~>创建tableView

    [self setupRefresh];//~>刷新

    

}

#pragma mark -  上下刷新

- (void)setupRefresh{

    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewComments)];

    [self.tableView.mj_header beginRefreshing];

    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreComments)];

    

}

#pragma mark - 上啦刷新,获取最新数据

- (void)loadNewComments{

 

    self.currentPage = 1;

    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    params[@"pageNo"] = @(self.currentPage);

    [self.manager.requestSerializer setValue:@"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" forHTTPHeaderField:@"Accept-Language"];

    [self.manager.requestSerializer setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];

    [self.manager.requestSerializer setValue:@"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" forHTTPHeaderField:@"User-Agent"];

    [self.manager.requestSerializer setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];

    [self.manager.requestSerializer setValue:self.string forHTTPHeaderField:@"Set-Cookie"];

    [self.manager GET:@"http://192.168.0.131:8081/lcbs/mobile/HomevisitsQuery.html" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {

        // 隐藏指示器

        [SVProgressHUD dismiss];

        NSLog(@"responseObject == %@",responseObject);

        // 服务器返回的JSON数据

        NSArray *message = [ModelHome mj_objectArrayWithKeyValuesArray:responseObject[@"result"]];

        self.categories = message;

        [self.tableView reloadData];

        [self.tableView.mj_header endRefreshing];

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

        

        [self.tableView.mj_header endRefreshing];

        [SVProgressHUD showErrorWithStatus:@"加载推荐信息失败!"];

    }];

}

#pragma mark - 加载更多数据

-(void)loadMoreComments{

    

    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    params[@"pageNo"] = @( ++self.currentPage);

    

    [self.manager.requestSerializer setValue:@"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" forHTTPHeaderField:@"Accept-Language"];

    [self.manager.requestSerializer setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];

    [self.manager.requestSerializer setValue:@"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" forHTTPHeaderField:@"User-Agent"];

    [self.manager.requestSerializer setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];

    [self.manager.requestSerializer setValue:self.string forHTTPHeaderField:@"Set-Cookie"];

    [self.manager GET:@"http://192.168.0.131:8081/lcbs/mobile/HomevisitsQuery.html" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {

        // 隐藏指示器

        [SVProgressHUD dismiss];

        NSLog(@"responseObject == %@",responseObject);

        // 服务器返回的JSON数据

        

        

        NSArray *message = [ModelHome mj_objectArrayWithKeyValuesArray:responseObject[@"result"]];

        [self.categories addObjectsFromArray:message];

        [self.tableView reloadData];

        [self.tableView.mj_footer endRefreshing];

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

        

        [self.tableView.mj_footer endRefreshing];

        [SVProgressHUD showErrorWithStatus:@"加载推荐信息失败!"];

    }];

}

#pragma mark - 创建tableView

-(void)setupTableView{

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 150+40+64+10, self.view.width, self.view.height - self.topView.height-64 -40-10 - self.secView.height) style:UITableViewStylePlain];

    [self.tableView registerNib:[UINib nibWithNibName:(NSStringFromClass([TableViewCell class])) bundle:nil] forCellReuseIdentifier:TableViewCellID];

    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    

    [self.view addSubview:self.tableView];

}

#pragma mark - tableView的代理方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableViewCellID];

        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        cell.modelHome = self.categories[indexPath.row];

        cell.delegate = self;

        return cell;

    

    

    

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 129;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    // 点击某一行时,创建一个新的控制器对象:

    ShowViewController *svc = [[ShowViewController alloc] init];

    

    NSLog(@"self.categories[indexPath.row]  %@",self.categories[indexPath.row]);

    ModelHome *model = self.categories[indexPath.row];

    svc.modelHome = model;

    

    [self.navigationController pushViewController:svc animated:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.categories.count;

}

#pragma mark - 创建第一个View

-(void)creatView1{

    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.width, 160)];

    self.topView.backgroundColor = [UIColor colorWithRed:0.88f green:0.88f blue:0.88f alpha:1.00f];

    [self.view addSubview:self.topView];

    NSArray *toplabelarray = @[@"承租人:",@"经销商:",@"申请编号:"];

    for (NSInteger i = 0 ; i < 3; i ++) {

        self.lesseelabel = [[UILabel alloc]initWithFrame:CGRectMake(10,  30*i +10*i, self.view.width/5, 30)];

        self.lesseelabel.text = toplabelarray[i];

        [self.topView addSubview:self.lesseelabel];

    }

    

    self.changeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 30 *3+10*3, self.view.width/5, 30)];

    self.changeLabel.text = @"自然人";

    [self.topView addSubview:self.changeLabel];

    self.mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(self.topView.width - self.view.width/5, 30 *3+10*3, self.topView.width/5, 30)];

    // 事件: UIControlEventValueChanged

    [self.mySwitch addTarget:self action:@selector(clickSwith:) forControlEvents:UIControlEventValueChanged];

    self.mySwitch.tag = 10;

    //设置打开的颜色:

    self.mySwitch.onTintColor = [UIColor whiteColor];

    // 边框颜色:

    self.mySwitch.tintColor = [UIColor colorWithRed:0.36f green:0.72f blue:0.36f alpha:1.00f];

    // 开关圆点的颜色:

    self.mySwitch.thumbTintColor = [UIColor blueColor];

    [self.topView addSubview:self.mySwitch];

}

#pragma mark - 创建textField

-(void)creatField{

    [self setupTextField:self.lesseelTextField  placehoder:@"请输入承租人名称" frame:CGRectMake(self.lesseelabel.width +20, 0,self.view.width - self.lesseelabel.width - 20-5, 30) textTag:1];

    [self setupTextField: self.dealersTextField  placehoder:@"请输入经销商名称" frame:CGRectMake(self.lesseelabel.width +20, 30 + 10,self.view.width - self.lesseelabel.width - 20-5, 30) textTag:2];

    [self setupTextField:self.numberTextField placehoder:@"请输入证件号名称" frame:CGRectMake(self.lesseelabel.width +20, 30+10+30+10,self.view.width - self.lesseelabel.width - 20-5, 30) textTag:3];

}

#pragma mark - 封装textField

-(void)setupTextField:(UITextField *)textfield placehoder:(NSString *)placehoder frame:(CGRect)frame textTag:(NSInteger)tag{

    textfield = [[UITextField alloc]initWithFrame:frame];

    textfield.placeholder = @"请输入经销商名称";

    textfield.borderStyle = UITextBorderStyleBezel;

    textfield.clearButtonMode = UITextFieldViewModeWhileEditing;

    [textfield addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];

    textfield.clearsOnBeginEditing = YES;

    textfield.tag = tag;

    

    [self.topView addSubview:textfield];

}

-(void)textFieldEditChanged:(UITextField *)text{

    if (text.tag == 1) {

        

        self.lesseelstring = text.text;

    }else if (text.tag == 2){

        self.dealersstring = text.text;

        

    }else if (text.tag == 3){

        self.numberstring = text.text;

        

    }

    

    

    

}

#pragma mark - 法人和自然人选择

- (void)clickSwith:(UISwitch *)s{

    if (s.isOn == 0) {

        self.changeLabel.text = @"自然人";

    }else{

        self.changeLabel.text = @"法人";

    }

    

}

#pragma mark - 创建第二个View

-(void)creatView2{

    self.secView = [[UIView alloc]initWithFrame:CGRectMake(0, 64+self.topView.height , self.view.width, 40)];

    self.secView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:self.secView];

    self.secView.userInteractionEnabled = YES;

    [self setupButton: self.addbutton title:@"新增" color:[UIColor blackColor] highlight:[UIColor redColor] frame:CGRectMake(0, 0  , self.secView.width / 2 -1, 40) btnTag:(1)];

    self.addbutton.tag = 1;

    [self setupButton:self.querybutton title:@"查询" color:[UIColor blackColor] highlight:[UIColor redColor] frame:CGRectMake(self.secView.width / 2+1 , 0, self.secView.width / 2-1 , 40) btnTag:(2)];

    self.querybutton.tag = 2;

    

    

    

    

    

}

#pragma mark - 查找和新增

-(void)search:(UIButton *)btn{

    if (btn.tag == 1) {

        return;

    }

    [self search];

    

    [self.view endEditing:YES];

    

}

#pragma mark - 查找信息代码

-(void)search{

    self.currentPage = 1;

    

    

    NSMutableDictionary *params = [NSMutableDictionary dictionary];

     params[@"pageNo"] = @(self.currentPage);

    params[@"customerName"] = self.lesseelstring;

    

    params[@"dealerName"] = self.dealersstring;

    params[@"bizCode"] = self.numberstring;

    

    [self.manager.requestSerializer setValue:@"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3" forHTTPHeaderField:@"Accept-Language"];

    [self.manager.requestSerializer setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];

    [self.manager.requestSerializer setValue:@"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" forHTTPHeaderField:@"User-Agent"];

    [self.manager.requestSerializer setValue:@"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];

    

    [self.manager.requestSerializer setValue:self.string forHTTPHeaderField:@"Set-Cookie"];

    [self.manager POST:@"http://192.168.0.131:8081/lcbs/mobile/HomevisitsQuery.html" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        

    } progress:^(NSProgress * _Nonnull uploadProgress) {

        

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        // 隐藏指示器

        [SVProgressHUD dismiss];

        NSLog(@"responseObject == %@",responseObject);

        // 服务器返回的JSON数

        [self.categories removeAllObjects];

        NSArray *message = [ModelHome mj_objectArrayWithKeyValuesArray:responseObject[@"result"]];

        [self.categories addObjectsFromArray:message];

        [self.tableView reloadData];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        

        [SVProgressHUD showErrorWithStatus:@"加载推荐信息失败!"];

    }];

    

}

#pragma mark - 封装button控件

-(void)setupButton:(UIButton *)btn title:(NSString *)title color:(UIColor *)color highlight:(UIColor *)highlight frame:(CGRect)frame btnTag:(NSInteger)tag{

    btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setTitle:title forState:UIControlStateNormal];

    [btn setTitle:title forState:UIControlStateHighlighted];

    [btn setTitleColor:color forState:UIControlStateNormal];

    [btn setTitleColor:highlight forState:UIControlStateHighlighted];

    btn.layer.cornerRadius = 8.0;

    [btn setBackgroundColor:[UIColor colorWithRed:0.36f green:0.72f blue:0.36f alpha:1.00f]];

    [btn addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];

    btn.frame = frame;

    btn.tag = tag;

    [self.secView addSubview:btn];

}

- (void)choseTerm:(UIButton *)button

{

    _clickIndex = button.tag;

    ShowViewController *svc = [[ShowViewController alloc]init];

    [self.navigationController pushViewController:svc animated:YES];

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: