XLForm怎么在iOS中使用-创新互联

本篇文章给大家分享的是有关XLForm怎么在iOS中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联公司是一家集网站建设,应城企业网站建设,应城品牌网站建设,网站定制,应城网站建设报价,网络营销,网络优化,应城网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

一、 导入项目

使用CocoaPods或者手动导入库文件,本人选择直接导入项目源文件的方式。

XLForm怎么在iOS中使用

导入项目.png

二、改造表单ViewController

让ViewController继承自XLFormViewController,并重写下面的两个方法

@interface OneViewController : XLFormViewController

@end


@implementation OneViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self){
    [self initializeForm];
  }
  return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
  self = [super initWithCoder:aDecoder];
  if (self){
    [self initializeForm];
  }
  return self;
}
@end

三、构造表单

- (void)initializeForm {
  
  // 设置是否显示Cell之间分界线
  //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  // 设置Section的高度
  self.tableView.sectionHeaderHeight = 30;
  
  XLFormDescriptor * form;//form,一个表单只有一个
  XLFormSectionDescriptor * section;//section,一个表单可能有多个
  XLFormRowDescriptor * row; //row,每个section可能有多个row
  
  // Form
  form = [XLFormDescriptor formDescriptor];

  
  // First section
  section = [XLFormSectionDescriptor formSection];
  section.title = @"用户";
  [form addFormSection:section];
  // 普通文本
  row = [XLFormRowDescriptor formRowDescriptorWithTag:@"username" rowType:XLFormRowDescriptorTypeText];
  // 设置placeholder
  [row.cellConfig setObject:@"用户名" forKey:@"textField.placeholder"];
  // 设置文本颜色
  [row.cellConfig setObject:[UIColor redColor] forKey:@"textField.textColor"];
  [section addFormRow:row];
  // 密码
  row = [XLFormRowDescriptor formRowDescriptorWithTag:@"password" rowType:XLFormRowDescriptorTypePassword];
  // 设置placeholder的颜色
  NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"密码" attributes:
                   @{NSForegroundColorAttributeName:[UIColor greenColor],
                    }];
  [row.cellConfig setObject:attrString forKey:@"textField.attributedPlaceholder"];
  [section addFormRow:row];
  
  
  
  // Second Section
  section = [XLFormSectionDescriptor formSection];
  section.title = @"日期";
  [form addFormSection:section];
  // 日期选择器
  row = [XLFormRowDescriptor formRowDescriptorWithTag:@"birthday" rowType:XLFormRowDescriptorTypeDate title:@"出生日期"];
  row.value = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
  [section addFormRow:row];
  
  
  
   // Third Section
  section = [XLFormSectionDescriptor formSection];
  section.title = @"头像";
  [form addFormSection:section];
  // 图片选择
  row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userpic" rowType:XLFormRowDescriptorTypeImage];
  [section addFormRow:row];
  
  
  
  // Fourth Section
  section = [XLFormSectionDescriptor formSection];
  section.title = @"选择器";
  [form addFormSection:section];
  // 选择器
  row = [XLFormRowDescriptor formRowDescriptorWithTag:@"sex" rowType:XLFormRowDescriptorTypeSelectorPush];
  row.noValueDisplayText = @"暂无";
  row.selectorTitle = @"性别选择";
  row.selectorOptions = @[@"男",@"女",@"其他"];
  row.title = @"性别";
  [row.cellConfigForSelector setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
  [row.cellConfigForSelector setObject:[UIColor greenColor] forKey:@"detailTextLabel.textColor"];
  [section addFormRow:row];
  
  
  
  // Fifth Section
  section = [XLFormSectionDescriptor formSection];
  section.title = @"加固";
  [form addFormSection:section];
  // 开关
  row = [XLFormRowDescriptor formRowDescriptorWithTag:@"enforce" rowType:XLFormRowDescriptorTypeBooleanSwitch title:@"加固"];
  [section addFormRow:row];
  
  
  // Sixth Section
  section = [XLFormSectionDescriptor formSection];
  [form addFormSection:section];
  // 按钮
  row = [XLFormRowDescriptor formRowDescriptorWithTag:@"conform" rowType:XLFormRowDescriptorTypeButton];
  row.title = @"确定";
  [section addFormRow:row];
  

  self.form = form;
}

-(void)didSelectFormRow:(XLFormRowDescriptor *)formRow{

  // 判断是不是点击了确定按钮
  if([formRow.tag isEqualToString:@"conform"] && formRow.rowType == XLFormRowDescriptorTypeButton){

    //获取表单所有到的值
    NSDictionary *values = [self formValues];

    NSLog(@"%@", values);

  }
  
  [super didSelectFormRow:formRow];

}

//重写改该方法 上面的方法就不会调用了
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//  NSLog(@"%s", __func__);
//
//}
@end

四、效果图

XLForm怎么在iOS中使用

效果图.png

五、总结

前面两步是官方文档中可以找到的,也很简单,关键在于initializeForm方法中具体构造表单的过程,这里有必要强调几点:

1.XLFormViewController实现了UITableViewDataSource, UITableViewDelegate,并且持有一个UITableView,这个从该类的声明可以看出来,所以UITableView 、UITableViewDataSource, UITableViewDelegate中的方法都可以正常使用。

复制代码 代码如下:

@interface XLFormViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, XLFormDescriptorDelegate, UITextFieldDelegate, UITextViewDelegate, XLFormViewControllerDelegate>

2.XLForm将表单抽象为Form,Section,Row三个层次,分别对应三个类

XLFormDescriptor * form;//form,一个表单只有一个
XLFormSectionDescriptor * section;//section,一个表单可能有多个
XLFormRowDescriptor * row; //row,每个section可能有多个row

3.每个表单中的具体信息最后都落脚到XLFormRowDescriptor中,通过它可以配置不同样式的表单项,通过构造函数的rowType指定具体的表单类型,该框架提供了非常丰富的rowType,具体可以参考官方文档说明。

4.更细化配置表单项就需要借助于XLFormRowDescriptor中的属性进行配置,常用的有

@property (nonatomic, readonly, nonnull) NSMutableDictionary * cellConfig;
@property (nonatomic, readonly, nonnull) NSMutableDictionary * cellConfigForSelector;

这个配置的时候,往往有同学不知道具体如何才能设置属性,比如怎么设置表单输入框的placeholder?更进一步如何设置placeholder 的颜色。其实它用到了KVC,因为它们两个都是UITextField类中的属性,那么直接进入UITextField查找,发现如下信息:

@property(nullable, nonatomic,copy)  NSString        *placeholder;  
@property(nullable, nonatomic,copy)  NSAttributedString   *attributedPlaceholder NS_AVAILABLE_IOS(6_0);

那么设置起来就是

[row.cellConfig setObject:@"用户名" forKey:@"textField.placeholder"];
 [row.cellConfig setObject:attrString forKey:@"textField.attributedPlaceholder"];

注意这里的key的写法,就是KVC的写法。其他的属性依此类推。

5.如何获取设置好的表单的值?其实非常简单,该框架提供一个方法formValues,它的返回类型是一个NSDictionary,其中key就是XLFormRowDescriptor设置时的Tag。可以直接在控制器中调用该方法获取表单值,上面的效果图设置后的表单信息如下:

XLForm怎么在iOS中使用

以上就是XLForm怎么在iOS中使用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联网站建设公司行业资讯频道。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

名称栏目:XLForm怎么在iOS中使用-创新互联
网站链接:https://www.cdcxhl.com/article2/cejsoc.html

成都网站建设公司_创新互联,为您提供ChatGPT面包屑导航网站设计网站设计公司云服务器App设计

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管