MAFormViewController 设计用于与 MATextFieldCells 配合使用,以实现快速、简单的基于 UITableView 的表单创建。它能自动处理表单配置、格式化、导航、验证和提交。
将 MAFormViewController.h/m, MAFormField.h/m, 和 MATextFieldCell.h/m 添加到您的项目中,并将 MAFormViewController.h 和 MAFormField.h 导入到您想要显示表单的任何视图控制器中。
MAFormViewController 由 MAFormField 对象的数组组成 - 这一行代码中封装了大量的重复代码和逻辑,使得您不需要反复编写。键用于以后识别每个字段用户输入的信息,类型(以下将提供更多详情)定义了将被创建的表单字段的类型,这包括键盘类型、数据域限制、初始值(如果您想将现有数据预填充到表单字段中)、每个字段的占位符,一个 BOOL 用于告诉表单是否希望“动画”占位符出现在文本字段上方,以便用户在文本出现后可以看到他们正在编辑的内容,以及一个 BOOL 用于验证表单以确保(或不是)该字段具有条目。
之后,您可以把这些 MAFormField 对象分组到数组中,代表表单的一部分。一个表单可以由任意数量的部分和字段组成。您只需创建所有字段,按您偏好的方式分组,并将它们传递给 MAFormViewController 的自定义 init 方法,该方法接受您创建的字段、用于提交/发送/保存表单信息的按钮标题,以及一个块,该块将使用您在创建字段时提供的键作为字典的键,以及用户在字段中输入的值作为与这些键相关联的值来表示表单。
MAFormField *nameField = [MAFormField fieldWithKey:@"name" type:MATextFieldTypeName initialValue:nil placeholder:@"Name" required:YES];
MAFormField *usernameField = [MAFormField fieldWithKey:@"username" type:MATextFieldTypeName initialValue:nil placeholder:@"Username" required:YES];
MAFormField *passwordField = [MAFormField fieldWithKey:@"password" type:MATextFieldTypePassword initialValue:nil placeholder:@"Password" required:YES];
// separate the cells into sections
NSArray *firstSection = @[nameField];
NSArray *secondSection = @[usernameField, passwordField];
NSArray *cellConfig = @[firstSection, secondSection];
// create the form and present it modally with its own navigation controller
MAFormViewController *formVC = [[MAFormViewController alloc] initWithCellConfigurations:cellConfig actionText:@"Save" animatePlaceholders:YES handler:^(NSDictionary *resultDictionary) {
// now that we're done, dismiss the form
[self dismissViewControllerAnimated:YES completion:nil];
// if we don't have a result dictionary, the user cancelled, rather than submitted the form
if (!resultDictionary) {
return;
}
// do whatever you want with the results - you can access specific values from the dictionary using
// the key you provided when you created the form
[[[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"Thanks for registering %@!", resultDictionary[@"name"]] delegate:nil cancelButtonTitle:@"Yay!" otherButtonTitles:nil] show];
}];
// optional - disable the unsaved changes warning
// formVC.warnForUnsavedChanges = NO;
// optional - override the default unsaved changes message
// formVC.unsavedChangesMessage = @"WAIT!!! You have unsaved changes!!";
UINavigationController *formNC = [[UINavigationController alloc] initWithRootViewController:formVC];
[self presentViewController:formNC animated:YES completion:nil];
结合 MAFormViewControllers 和 MATextFieldCells 的便利和易用性,可以免除您处理与创建表单相关的最繁琐和重复性任务的需求。以下所有操作都自动为您完成:
默认
姓名
电话
电子邮件
地址
StateAbbr
ZIP
数字
十进制
日期
密码
网址
非可编辑
无
下一步
完成
在 Xcode 中打开项目,选择模拟器,然后按 command-U。
欢迎提出问题、评论、问题和拉取请求!感谢 Whelton 为 Cocoapods 集成,感谢 jverdi 的出色项目(https://github.com/jverdi/JVFloatLabeledTextField)为动画占位符提供灵感。
本项目按照 MIT 许可协议提供。有关详细信息,请参阅 LICENSE.txt。