ios / iphone sdk表单管理最佳实践

时间:2023-01-19 20:07:31

I'm working on an iPhone app that will have involve a lot of forms. Currently I have a ViewController class for each settings page which has an UITableView loaded with possible settings. When someone clicks on a setting they are taken taken to a new view to enter the form value, or allowed to enter things in place.

我正在开发一款涉及很多表格的iPhone应用程序。目前我有一个ViewController类用于每个设置页面,其中有一个UITableView加载了可能的设置。当有人点击某个设置时,他们会被带到新视图以输入表单值,或者允许他们输入相应的内容。

What's the best way to keep things DRY? What pieces of this implementation could be implemented once and re-used?

保持干燥的最佳方法是什么?这个实现的哪些部分可以实现一次并重新使用?

When someone clicks on a settings option which goes to a new view how can I create this view and add a text field according to the data type (uitextfield or picker or something else) in code?

当有人点击进入新视图的设置选项时,如何创建此视图并根据代码中的数据类型(uitextfield或picker或其他内容)添加文本字段?

2 个解决方案

#1


6  

You can programmatically:

您可以通过编程方式:

create view hierarchy

创建视图层次结构

UIButton

UILabel

You get the idea.

你明白了。

However, I would recommend getting your logic working for a few of the cases, and then it should become obvious what parts are redundant as you find yourself typing in the same thing over and over. At that point, refactor to get the redundant code into a re-usable form.

但是,我建议让你的逻辑工作在一些情况下,然后很明显哪些部分是多余的,因为你发现自己一遍又一遍地输入相同的东西。此时,重构将冗余代码转换为可重用的形式。

HTH.

#2


1  

If you have a tableView, the flow is to create the viewController of the selected settings in the didSelectCell method of your tableView delegate and to push it through the current viewController's navigation controller.

如果你有一个tableView,那么流程就是在tableView委托的didSelectCell方法中创建所选设置的viewController,并将其推送到当前的viewController导航控制器。

here's a sample:

这是一个样本:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController pushViewController:[self settingsViewControllerAtIndexPath:indexPath] animated:YES];
}

so you'll have to implement the method:

所以你必须实现这个方法:

 - (UIViewController*)settingsViewControllerAtIndexPath:(NSIndexPath *)indexPath;

wich will return the viewController managing the settings associated with the selected row of your root tableView.

将返回viewController,管理与根tableView的选定行关联的设置。

If your forms are pretty statics, you should consider using a xib in order to minimize the amount of code needed. it isn't a perfect answer to your "how to keep DRY" but it's neat enough ;)

如果您的表单非常静态,则应考虑使用xib以最小化所需的代码量。它不是你“如何保持干燥”的完美答案,但它足够整洁;)

Good luck.

#1


6  

You can programmatically:

您可以通过编程方式:

create view hierarchy

创建视图层次结构

UIButton

UILabel

You get the idea.

你明白了。

However, I would recommend getting your logic working for a few of the cases, and then it should become obvious what parts are redundant as you find yourself typing in the same thing over and over. At that point, refactor to get the redundant code into a re-usable form.

但是,我建议让你的逻辑工作在一些情况下,然后很明显哪些部分是多余的,因为你发现自己一遍又一遍地输入相同的东西。此时,重构将冗余代码转换为可重用的形式。

HTH.

#2


1  

If you have a tableView, the flow is to create the viewController of the selected settings in the didSelectCell method of your tableView delegate and to push it through the current viewController's navigation controller.

如果你有一个tableView,那么流程就是在tableView委托的didSelectCell方法中创建所选设置的viewController,并将其推送到当前的viewController导航控制器。

here's a sample:

这是一个样本:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationController pushViewController:[self settingsViewControllerAtIndexPath:indexPath] animated:YES];
}

so you'll have to implement the method:

所以你必须实现这个方法:

 - (UIViewController*)settingsViewControllerAtIndexPath:(NSIndexPath *)indexPath;

wich will return the viewController managing the settings associated with the selected row of your root tableView.

将返回viewController,管理与根tableView的选定行关联的设置。

If your forms are pretty statics, you should consider using a xib in order to minimize the amount of code needed. it isn't a perfect answer to your "how to keep DRY" but it's neat enough ;)

如果您的表单非常静态,则应考虑使用xib以最小化所需的代码量。它不是你“如何保持干燥”的完美答案,但它足够整洁;)

Good luck.