时时监听textfield的值的变化

来源:互联网 发布:javascript编写软件 编辑:程序博客网 时间:2024/05/10 17:50

#import "mendchildViewController.h"


@interface mendchildViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>

@property(nonatomic,strong)UITableView *onetableview;

@end

@implementation mendchildViewController

-(void)viewDidAppear:(BOOL)animated{

 self.navigationItem.title =@"修改孩子信息"; self.navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColorcolorWithRed:55.0/255 green:55.0/255 blue:55.0/255 alpha:1]};

 self.edgesForExtendedLayout =UIRectEdgeNone;

 [self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"sk2"]forBarMetrics:UIBarMetricsDefault];

 [self.navigationController.navigationBarsetShadowImage:[UIImagenew]];

 [selfsetNeedsStatusBarAppearanceUpdate];

 self.navigationController.navigationBar.translucent =NO;

 [[UIBarButtonItemappearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)forBarMetrics:UIBarMetricsDefault];

}


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

 self.view.backgroundColor = [UIColorcolorWithRed:242.0/255green:242.0/255blue:242.0/255alpha:1];

 _onetableview = [[UITableViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,325) style:UITableViewStylePlain];

 _onetableview.delegate =self;

 _onetableview.dataSource =self;

 _onetableview.backgroundColor = [UIColororangeColor];

 [self.viewaddSubview:_onetableview];

 _onetableview.tableFooterView = [[UIViewalloc]initWithFrame:CGRectZero];

 _redbtn = [[UIButtonalloc]init];

 [_redbtnsetTitle:@"保存"forState:UIControlStateNormal];

 _redbtn.layer.cornerRadius =2.0;

 _redbtn.backgroundColor =weizicolor;

 [_redbtnaddTarget:selfaction:@selector(clickred)forControlEvents:UIControlEventTouchUpInside];

 [self.viewaddSubview:_redbtn];

 [_redbtnmas_makeConstraints:^(MASConstraintMaker *make) {

  make.centerX.mas_equalTo(self.view.mas_centerX);

  make.top.mas_equalTo(345);

  make.left.mas_equalTo(40 *screenRation);

  make.right.mas_equalTo(-40 *screenRation);

 }];

 

}

//从相册中读取

-(void)readImageFromAlbum{

 //创建对象

 UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc] init];

 //(选择类型)表示仅仅从相册中选取照片

 imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

 //指定代理,因此我们要实现UIImagePickerControllerDelegate,UINavigationControllerDelegate协议

 imagePicker.delegate = self;

 //设置在相册选完照片后,是否跳到编辑模式进行图片剪裁。(允许用户编辑)

 imagePicker.allowsEditing = YES;

 //显示相册

 [selfpresentViewController:imagePicker animated:YEScompletion:nil];

}

-(void)readImageFromCamera{

 if ([UIImagePickerControllerisSourceTypeAvailable:

      UIImagePickerControllerSourceTypeCamera]) {

  UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc] init];

  imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;

  imagePicker.delegate = self;

  imagePicker.allowsEditing = YES; //允许用户编辑

  [selfpresentViewController:imagePicker animated:YEScompletion:nil];

 } else {

  //弹出窗口响应点击事件

  UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"警告"message:@"未检测到摄像头" preferredStyle:UIAlertControllerStyleAlert];

  UIAlertAction *action = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleCancelhandler:nil];

  [alertController addAction:action];

  [selfpresentViewController:alertController animated:YEScompletion:nil];

 }

}


//图片完成之后处理

- (void)imagePickerController:(UIImagePickerController *)picker

        didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

 

 //image 就是修改后的照片

 //将图片添加到对应的视图上

 //初始化imageNew为从相机中获得的--

 _headview.image = image;

 [selfdismissViewControllerAnimated:YEScompletion:nil];

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

 return 1;

}

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

 return 6;

}

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

 if (indexPath.row==0) {

  return 100;

 }

 return 45;

}

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

 if (indexPath.row==0) {

  UITableViewCell *viewcell = [UITableViewCellnew];

  _headview = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"baby"]];

  _headview.layer.cornerRadius =30.0;

  _headview.layer.masksToBounds =YES;

  [_headviewaddTapGestureRecognizerWith:selfaction:@selector(clickhead:)];

  [viewcell.contentView addSubview:_headview];

  [_headviewmas_makeConstraints:^(MASConstraintMaker *make) {

   make.centerX.mas_equalTo(viewcell.contentView.mas_centerX);

  // make.centerY.mas_equalTo(viewcell.contentView.mas_centerY);

   make.size.mas_equalTo(CGSizeMake(60,60));

   make.top.mas_equalTo(10);

  }];

  _bottomLabel = [[UILabelalloc]init];

  _bottomLabel.text =@"点击修改头像";

  _bottomLabel.textColor =ILuColor(51,51, 51);

  _bottomLabel.textAlignment =NSTextAlignmentCenter;

  _bottomLabel.font = [UIFontsystemFontOfSize:24*screenRation];

  [viewcell.contentView addSubview:_bottomLabel];

  [_bottomLabelmas_makeConstraints:^(MASConstraintMaker *make) {

   make.centerX.mas_equalTo(viewcell.mas_centerX);

   make.top.mas_equalTo(_headview.mas_bottom).offset(10);

   make.size.mas_equalTo(CGSizeMake(80,12));

  }];

  return viewcell;

 }else if (indexPath.row==1){

  UITableViewCell *viewcell = [UITableViewCellnew];

  UILabel *label = [[UILabelalloc]init];

  label.text = @"孩子昵称:";

  label.textAlignment =NSTextAlignmentLeft;

  label.font = [UIFontsystemFontOfSize:32*screenRation];

  label.textColor = ILuColor(51, 51, 51);

  [viewcell addSubview:label];

  [label mas_makeConstraints:^(MASConstraintMaker *make) {

   make.left.mas_equalTo(40 *screenRation);

   make.centerY.mas_equalTo(viewcell.mas_centerY);

   make.size.mas_equalTo(CGSizeMake(120,12));

  }];

  _namefield = [[UITextFieldalloc]init];

  //_namefield.text = @"雨";

  _namefield.placeholder =@"昵称";

  _namefield.textAlignment=NSTextAlignmentRight;

  _namefield.backgroundColor = [UIColorclearColor];

  _namefield.textColor = [UIColorblackColor];

  _namefield.font = [UIFontsystemFontOfSize:18];

  _namefield.delegate =self;

  [_namefieldaddTarget:selfaction:@selector(valuechanged:)forControlEvents:UIControlEventAllEditingEvents];

  [viewcell addSubview:_namefield];

  [_namefieldmas_makeConstraints:^(MASConstraintMaker *make) {

   make.right.mas_equalTo(-40 *screenRation);

   make.centerY.mas_equalTo(viewcell.mas_centerY);

   make.size.mas_equalTo(CGSizeMake(120,18));

  }];

  return viewcell;

 }else if (indexPath.row==2){

  UITableViewCell *viewcell = [UITableViewCellnew];

  UILabel *label = [[UILabelalloc]init];

  label.text = @"孩子年龄:";

  label.textAlignment =NSTextAlignmentLeft;

  label.font = [UIFontsystemFontOfSize:32*screenRation];

  label.textColor = ILuColor(51, 51, 51);

  [viewcell addSubview:label];

  [label mas_makeConstraints:^(MASConstraintMaker *make) {

   make.left.mas_equalTo(40 *screenRation);

   make.centerY.mas_equalTo(viewcell.mas_centerY);

   make.size.mas_equalTo(CGSizeMake(120,12));

  }];

  _yearfield = [[UITextFieldalloc]init];

  //_yearfield.text = @"2岁";

  _yearfield.placeholder =@"2岁";

  _yearfield.textAlignment=NSTextAlignmentRight;

  _yearfield.backgroundColor = [UIColorclearColor];

  //_namefield.backgroundColor = [UIColor redColor];

  _yearfield.textColor = [UIColorblackColor];

  _yearfield.font = [UIFontsystemFontOfSize:18];

  _yearfield.delegate =self;

  [viewcell addSubview:_yearfield];

  [_yearfieldmas_makeConstraints:^(MASConstraintMaker *make) {

   make.centerY.mas_equalTo(viewcell.mas_centerY);

   make.right.mas_equalTo(-40 *screenRation);

   make.size.mas_equalTo(CGSizeMake(120,18));

  }];

  return viewcell;

 }else if (indexPath.row==3){

  UITableViewCell *viewcell = [UITableViewCellnew];

  UILabel *label = [[UILabelalloc]init];

  label.text = @"孩子性别:";

  label.textAlignment =NSTextAlignmentLeft;

  label.font = [UIFontsystemFontOfSize:32*screenRation];

  label.textColor = ILuColor(51, 51, 51);

  [viewcell addSubview:label];

  [label mas_makeConstraints:^(MASConstraintMaker *make) {

   make.left.mas_equalTo(40 *screenRation);

   make.centerY.mas_equalTo(viewcell.mas_centerY);

   make.size.mas_equalTo(CGSizeMake(120,12));

  }];

  _sexfield = [[UITextFieldalloc]init];

  //_sexfield.text = @"性别";

  _sexfield.placeholder =@"性别";

  _sexfield.textAlignment=NSTextAlignmentRight;

  _sexfield.textColor =ILuColor(51,51, 51);

  _sexfield.font = [UIFontsystemFontOfSize:18];

  _sexfield.delegate =self;

 // [_sexfield addTarget:self action:@selector(clickfield:) forControlEvents:UIControlEventValueChanged];

  [viewcell addSubview:_sexfield];

  [_sexfieldmas_makeConstraints:^(MASConstraintMaker *make) {

   make.right.mas_equalTo(-40 *screenRation);

   make.centerY.mas_equalTo(viewcell.mas_centerY);

   make.size.mas_equalTo(CGSizeMake(120,18));

  }];

  return viewcell;

 }else if (indexPath.row==4){

  UITableViewCell *viewcell = [UITableViewCellnew];

  UILabel *label = [[UILabelalloc]init];

  label.text =@"孩子体重(kg):";

  label.textAlignment =NSTextAlignmentLeft;

  label.font = [UIFontsystemFontOfSize:32*screenRation];

  label.textColor = ILuColor(51, 51, 51);

  [viewcell addSubview:label];

  [label mas_makeConstraints:^(MASConstraintMaker *make) {

   make.left.mas_equalTo(40 *screenRation);

   make.centerY.mas_equalTo(viewcell.mas_centerY);

   make.size.mas_equalTo(CGSizeMake(120,12));

  }];

  

  _weightfield = [[UITextFieldalloc]init];

  //_weightfield.text = @"25.0";

  _weightfield.placeholder =@"体重";

  _weightfield.textAlignment=NSTextAlignmentRight;

  _weightfield.textColor =ILuColor(51,51, 51);

  _weightfield.font = [UIFontsystemFontOfSize:18];

  _weightfield.delegate  =self;

  [viewcell addSubview:_weightfield];

  [_weightfieldmas_makeConstraints:^(MASConstraintMaker *make) {

   make.right.mas_equalTo(-40 *screenRation);

   make.centerY.mas_equalTo(viewcell.mas_centerY);

   make.size.mas_equalTo(CGSizeMake(120,18));

  }];

  return viewcell;

 }

 UITableViewCell *viewcell = [UITableViewCellnew];

 UILabel *label = [[UILabelalloc]init];

 label.text =@"孩子身高(cm):";

 label.textAlignment =NSTextAlignmentLeft;

 label.font = [UIFontsystemFontOfSize:32*screenRation];

 label.textColor = ILuColor(51, 51, 51);

 [viewcell addSubview:label];

 [label mas_makeConstraints:^(MASConstraintMaker *make) {

  make.left.mas_equalTo(40 *screenRation);

  make.centerY.mas_equalTo(viewcell.mas_centerY);

  make.size.mas_equalTo(CGSizeMake(120,12));

 }];

 _heightfield = [[UITextFieldalloc]init];

 //_heightfield.text = @"36.0";

 _heightfield.placeholder =@"身高";

 _heightfield.textAlignment=NSTextAlignmentRight;

 _heightfield.textColor =ILuColor(51,51, 51);

 _heightfield.font = [UIFontsystemFontOfSize:18];

 _heightfield.delegate =self;

 [viewcell addSubview:_heightfield];

 [_heightfieldmas_makeConstraints:^(MASConstraintMaker *make) {

  make.right.mas_equalTo(-40 *screenRation);

  make.centerY.mas_equalTo(viewcell.mas_centerY);

  make.size.mas_equalTo(CGSizeMake(120,18));

 }];

 return viewcell;


}

-(void)valuechanged:(UITextField *)textfield{

 NSLog(@"输出textfield的值:%@",textfield.text);

}

//-(void)textFieldDidEndEditing:(UITextField *)textField{

// UITableViewCell *cell = (UITableViewCell*)[textField superview];

// NSIndexPath *indexPath = [_onetableview indexPathForCell:cell];

//   if (indexPath.row==1) {

//    NSLog(@"输出textfield.text的值:%@",textField.text);

//   }else if (indexPath.row==2){

//    NSLog(@"输出textfield.text的值:%@",textField.text);

//   }else if (indexPath.row==3){

//    NSLog(@"输出textfield.text的值:%@",textField.text);

//   }else if (indexPath.row==4){

//    NSLog(@"输出textfield.text的值:%@",textField.text);

//   }else if (indexPath.row==5){

//    NSLog(@"输出textfield.text的值:%@",textField.text);

//   }

//}

-(void)textFieldDidBeginEditing:(UITextField *)textField{

 UITableViewCell *cell = (UITableViewCell *)[textFieldsuperview];

 NSIndexPath *indexPath = [_onetableviewindexPathForCell:cell];

 if (indexPath.row==1) {

  NSLog(@"输出textfield.text的值:%@",textField.text);

 }else if (indexPath.row==2){

  NSLog(@"输出textfield.text的值:%@",textField.text);

 }else if (indexPath.row==3){

  NSLog(@"输出textfield.text的值:%@",textField.text);

 }else if (indexPath.row==4){

  NSLog(@"输出textfield.text的值:%@",textField.text);

 }else if (indexPath.row==5){

  NSLog(@"输出textfield.text的值:%@",textField.text);

 }

}

-(void)clickhead:(UITapGestureRecognizer *)sender{

 //NSLog(@"点击的是head");

  UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"提示"message:nilpreferredStyle:UIAlertControllerStyleActionSheet];

  UIAlertAction *imageAction = [UIAlertActionactionWithTitle:@"相册"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

   [selfreadImageFromAlbum];

  }];

  UIAlertAction *cameraAction = [UIAlertActionactionWithTitle:@"相机"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

   [selfreadImageFromCamera];

  }];

  UIAlertAction *cancleAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

  [alertController addAction:imageAction];

  [alertController addAction:cameraAction];

  [alertController addAction:cancleAction];

  [selfpresentViewController:alertController animated:YEScompletion:^{

  }];

}

-(void)clickred{

 NSLog(@"保存");

}

-(void)clickbtn2{

 NSLog(@"点击的是头像2");

}


原创粉丝点击