【11/02】 iOS开发成长之路,【控件初始化方法封】
来源:互联网 发布:linux临时设置环境变量 编辑:程序博客网 时间:2024/04/19 09:46
封装控件的初始化方法
在平时的工作中,养成良好的编码习惯,命名风格,对以后的工作真的很重要。
总结了几点
- 尽量把重复的代码抽取出来
- 需要的传递参数拿出来
- 返回的参数
优化代码
以下是一个注册的简单界面,自己试着做了代码的重构,不断的重构才能让代码更加精简。虽然还是有很多重复的代码。但是应该比以前好点了吧。
#define DefaultHeight 44 // 每行高度#define textMarginLeft self.view.width*0.2 // 文字左边距#define iconSize 30 // 图标尺寸 30*30#define iconMarginLeft self.view.width*0.1 // 图标左边距#define textWidth self.view.width*0.6 // text的宽度// 输入框默认尺寸#define textFieldDefaultSize CGRectMake(textMarginLeft, i*DefaultHeight, textWidth, DefaultHeight)// 提示文本及按钮默认尺寸#define labelDefaultSize CGRectMake(0, (i)*DefaultHeight, self.view.width, DefaultHeight )@interface DSNewRegisterViewController (){ UILabel *agreeLabel;}@end@interface DSNewRegisterViewController ()@end@implementation DSNewRegisterViewController- (void)viewDidLoad { [super viewDidLoad]; [self initView]; // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)initView{ NSArray *labelArray=@[@"设置一个昵称吧",@"手机号",@"设置登录密码",@"",@"设置支付密码"]; NSArray *left_iconArray = @[@"icon_11",@"icon_12",@"icon_13",@"icon_19",@"icon_14"]; // 左边图标 NSArray *right_iconArray = @[@"icon_05",@"icon_06"]; //右边图标 self.navigationItem.title = @"注册"; UIView *labelBaseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, DefaultHeight*7)]; labelBaseView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:labelBaseView]; for (int i = 0; i < 7; i++) { if (i<2) { // 昵称 手机号 [self initLabelType1:labelBaseView withFrame:textFieldDefaultSize withText:labelArray[i] withIcon:left_iconArray[i] textFieldIndex:i]; } else if (i == 2 || i == 4) { // 密码 支付密码 [self initLabelType2:labelBaseView withFrame:textFieldDefaultSize withText:labelArray[i] withLeftIcon:left_iconArray[i] withRightIconON:right_iconArray[0] withRightIconOFF:right_iconArray[1] textFieldIndex:i]; } else if (i == 3) { // 提醒文字 [self initTipLabel:labelBaseView withFrame:labelDefaultSize withTipIcon:left_iconArray[i] textFieldIndex:i]; } else if (i == 5) { // 服务协议 button [self initAgreeBtn:labelBaseView withFrame:labelDefaultSize textFieldIndex:i]; }else if (i == 6) { // 提交 button [self initSubmitBtn:labelBaseView withFrame:labelDefaultSize textFieldIndex:i]; } }}// 一张图片的label- (void)initLabelType1:(UIView*)view withFrame:(CGRect)frame withText:(NSString*)placeholder withIcon:(NSString*)icon textFieldIndex:(NSInteger)index{ UIImageView *iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:icon]]; iconView.frame = CGRectMake(iconMarginLeft, index *DefaultHeight+5 , iconSize, iconSize); iconView.contentMode =UIViewContentModeScaleAspectFit; UITextField *field=[[UITextField alloc] initWithFrame:frame]; field.textAlignment=NSTextAlignmentLeft; field.font=[UIFont systemFontOfSize:14]; if (index == 1) { field.keyboardType = UIKeyboardTypePhonePad ; } field.placeholder=placeholder; field.delegate = self; [self drawLine:view withFrame:CGRectMake(0, DefaultHeight*index + DefaultHeight -1, self.view.width, 1)]; [view addSubview:iconView]; [view addSubview:field];}// 两张图片的label- (void)initLabelType2:(UIView*)view withFrame:(CGRect)frame withText:(NSString*)placeholder withLeftIcon:(NSString*)leftIcon withRightIconON:(NSString*)rightIconON withRightIconOFF:(NSString*)rightIconOFF textFieldIndex:(NSInteger)index{ UIImageView *iconLeftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:leftIcon]]; iconLeftView.frame = CGRectMake(iconMarginLeft, index *DefaultHeight+5 , iconSize, iconSize); iconLeftView.contentMode =UIViewContentModeScaleAspectFit; UIButton* iconRightBtn= [[UIButton alloc]initWithFrame:CGRectMake(self.view.width*0.8, index *DefaultHeight , 44, 43)]; [iconRightBtn setImage:[UIImage imageNamed:rightIconON] forState:UIControlStateNormal]; [iconRightBtn setImage:[UIImage imageNamed:rightIconON] forState:UIControlStateHighlighted]; [iconRightBtn setImage:[UIImage imageNamed:rightIconOFF] forState:UIControlStateSelected]; iconRightBtn.contentMode =UIViewContentModeScaleAspectFit; [iconRightBtn addTarget:self action:@selector(agreeClick:) forControlEvents:UIControlEventTouchUpInside]; UITextField *field=[[UITextField alloc] initWithFrame:frame]; field.textAlignment=NSTextAlignmentLeft; field.font=[UIFont systemFontOfSize:14]; field.placeholder=placeholder; field.delegate = self; field.keyboardType = UIKeyboardTypeDefault; [self drawLine:view withFrame:CGRectMake(0, DefaultHeight*index + DefaultHeight -1, self.view.width, 1)]; [view addSubview:iconLeftView]; [view addSubview:iconRightBtn]; [view addSubview:field];}// 提醒文字- (void)initTipLabel:(UIView *)view withFrame:(CGRect)frame withTipIcon:(NSString *)icon textFieldIndex:(NSInteger)index{ UIView *tipView = [[UIView alloc]initWithFrame:frame]; tipView.backgroundColor = DSColor(239, 239, 239); UIImageView *tipIconView = [[UIImageView alloc]init]; tipIconView.contentMode =UIViewContentModeScaleAspectFit; tipIconView.frame = CGRectMake(iconMarginLeft+10, iconSize/2, iconSize/2, iconSize/2); tipIconView.image = [UIImage imageNamed:icon]; UILabel *tipLabel=[[UILabel alloc] initWithFrame:frame]; tipLabel.frame = CGRectMake(textMarginLeft, 0, self.view.width-textMarginLeft, 43); tipLabel.text= @"登录密码和支付密码均为6-20位,数字,字母及组合"; tipLabel.textColor = [UIColor redColor]; tipLabel.numberOfLines = 0; tipLabel.font =[UIFont systemFontOfSize:13]; [self drawLine:view withFrame:CGRectMake(0, DefaultHeight*index + DefaultHeight -1, self.view.width, 1)]; [tipView addSubview:tipIconView]; [tipView addSubview:tipLabel]; [view addSubview:tipView];}// 画线- (void)drawLine:(UIView *)view withFrame:(CGRect)frame{ UIView *line=[[UIView alloc] initWithFrame:frame]; line.backgroundColor=[UIColor colorWithWhite:0.85 alpha:1.0]; [view addSubview:line];}// 协议Button- (void)initAgreeBtn:(UIView *)view withFrame:(CGRect)frame textFieldIndex:(NSInteger)index{ UIView * agreeView = [[UIView alloc]initWithFrame:frame]; agreeView.backgroundColor = DSColor(239, 239, 239); UIButton* iconView = [[UIButton alloc]init]; iconView.frame = CGRectMake(iconMarginLeft, 0, iconMarginLeft, 43); [iconView setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal]; [iconView setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected]; [iconView addTarget:self action:@selector(agreeClick:) forControlEvents:UIControlEventTouchUpInside]; [iconView setImageEdgeInsets:UIEdgeInsetsMake(10, 5,10 ,5)]; agreeLabel=[[UILabel alloc] initWithFrame:frame]; agreeLabel.frame = CGRectMake(textMarginLeft, 0, self.view.width-iconMarginLeft, 43); agreeLabel.text= @"我已阅读并同意《得仕钱包服务协议》"; agreeLabel.textColor = [UIColor blackColor]; agreeLabel.font =[UIFont systemFontOfSize:13]; [agreeView addSubview:iconView]; [agreeView addSubview:agreeLabel]; [view addSubview:agreeView];}// 提交Button- (void)initSubmitBtn:(UIView *)view withFrame:(CGRect)frame textFieldIndex:(NSInteger)i{ UIView *submitView = [[UIView alloc]initWithFrame:frame]; submitView.backgroundColor = DSColor(239, 239, 239); UIButton * submitBtn=[self LoadDefaultButtonInView:self.view withFrame:CGRectMake(textMarginLeft, 0, textWidth, 30) title:@"提交"]; [submitBtn addTarget:self action:@selector(submitBtnClick) forControlEvents:UIControlEventTouchUpInside]; [submitBtn.layer setMasksToBounds:YES]; [submitBtn.layer setCornerRadius:5.0]; [submitView addSubview:submitBtn]; [view addSubview:submitView];}#pragma mark - 方框选中按钮点击事件 明文密文切换点击事件- (void)agreeClick:(UIButton *)btn{ btn.selected = !btn.selected;}#pragma mark - 提交按钮点击事件- (void)submitBtnClick{ NSLog(@"Clicked on this button"); DSRegistNextViewController *tViewCtr=[[DSRegistNextViewController alloc]init]; [self.navigationController pushViewController:tViewCtr animated:YES];}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
0 0
- 【11/02】 iOS开发成长之路,【控件初始化方法封】
- IOS成长之路-Segmentarray控件
- IOS成长之路-UIAlertView控件
- IOS成长之路-UIDatePicker 控件
- 【11/04】 iOS开发成长之路,【tabelView cell复用】
- 【11/05】 iOS开发成长之路,【字符串NSString中去掉空格,textField代理方法 限制输入长度】
- IOS成长之路
- iOS开发之基础类的简单初始化方法
- iOS开发零基础教程之UILabel的新初始化方法
- iOS开发之OC继承、初始化方法、便利构造器
- 【10/26】 iOS开发成长之路,【从零开始】
- 【10/27】 iOS开发成长之路,【cell重用】
- 【10/28】 iOS开发成长之路,【tableView】
- IOS成长之路-去掉屏幕键盘的方法
- IOS成长之路-Nsstring中搜索方法rangeOfString
- IOS成长之路-Nsstring中搜索方法rangeOfString
- IOS成长之路-Nsstring中搜索方法rangeOfString
- IOS成长之路-Nsstring中搜索方法rangeOfString
- ios 中心放大动画
- 20分钟自动安装kilo版本openstack开发环境
- 判断请求URL中是IP还是域名
- ios高级工程师开发面试题
- 在Linux里设置环境变量的方法(export PATH)
- 【11/02】 iOS开发成长之路,【控件初始化方法封】
- -Dmaven.multiModuleProjectDirectory system propery is not set.IDEA14 中Maven报错
- 利用C++优势快速打完最长不下降子序列
- Windows远程到Linux
- AbstractList 抽象类 源代码
- Android源码分析-资源加载机制解析
- 一些知识点的初步理解_2(流形学习,ing...)
- 【日常学习】【数学】【众数】codevs4419 FFF团卧底的菊花题解
- 高端的N 校联考题