将选项卡式 UITableViewController 拆分为多个文件的好方法是什么?

发布于 2024-12-01 08:08:41 字数 534 浏览 0 评论 0原文

我已经实现了一个选项卡式UITableViewController。顶部有一些选项卡可以重新加载表格的内容。根据所选选项卡,向用户显示不同的单元格。一切都很好,但我最终得到了一个包含 3 个不同实现的源文件,并且变得有点庞大和混乱,甚至使用编译指示来标记源代码的各个部分。

我考虑过在运行时根据所选选项卡从字符串创建选择器,然后将 .m 文件拆分为多个文件,将重命名的方法放在那里,但随后会出现强制的 @end 和文件末尾以及编译器告诉您缺少要实现的方法。

确实,看起来 Objective-C 并不是为了将源代码分割成多个文件而设计的。有没有可以用于此目的的设计模式?不知何故,我设法在主类的 @end 之前使用 #include 来模拟所有这些,但它看起来不太漂亮。另外,如果我尝试将该文件包含到项目中,Xcode 会感到非常困惑,因为它尝试单独编译它(至少我可以将文件包含在项目中并禁用它们包含在目标中)。

I've implemented a tabbed UITableViewController. There are some tabs on the top which reload the contents of the table. Based on the selected tab different cells are shown to the user. It all works nice, but I end up with a source file which contains 3 different implementations, and gets a little bit bulky and confusing, even using pragmas to mark sections of the source code.

I've thought of creating selectors at runtime from strings based on the selected tab, then splitting the .m file into several putting there the renamed methods, but then there's the forced @end and the end of a file and the compiler telling you that there are missing methods to be implemented.

Really, it looks like objective-c wasn't designed to split the source though several files. Is there any design pattern that can be used for this? Somehow I managed to emulate all this using #include <otherfile.m> before the @end of the main class, but it doesn't look pretty. Also, Xcode gets confused as hell if I try to include that file into the project, since it tries to compile it separately (at least I can include the files in the project and disable their inclusion in the target).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弄潮 2024-12-08 08:08:41

最后我求助于基本的预处理器,这意味着包括和一些技巧。这就是我所做的:

  1. 给定一个表视图控制器 filename.m,为每个选项卡/相关功能组创建一个 filename_functionality.m 文件,并将方法放在那里。请注意,此文件中没有 @implementation@end#include 等,只有方法。
  2. 在您的 Xcode 项目中添加新文件,但请注意不要将它们标记为任何构建目标所需的文件。如果您忘记了,请稍后选择文件并取消选中将它们包含到目标中的标记。这是为了避免 Xcode 尝试单独编译这些 rouge 文件。
  3. 在主 filename.m 的末尾和 @end 标记之前,添加必要的 #include "filename_functionity.m",其中有效地告诉预处理器将所有文件视为一个文件。
  4. 对于您的文件调用从属文件的方法的情况,请在文件的顶部添加一个匿名接口并在那里声明您移动到其他文件的方法的原型。

此时,就可以了!然而,Xcode 仍然很烦人,尽管能够打开下级源代码文件,但它无法正确解析它们,因此您无法使用快速导航栏在方法之间跳转,例如例子。再次,为了解决这个更多的预处理器技巧:

  1. 在每个从属文件的开头,添加(糟糕,wiki 格式已损坏):

    #ifndef _INCLUDE_FILES

    @implementation BIEntity_view_controller

    #endif

  2. 从属文件末尾重复使用ifndef括起来@end

  3. 文件末尾和包含文件之前,定义_INCLUDE_FILES

它的作用是欺骗 XCode 认为这是一个正确的实现文件,因此语法突出显示、完成和导航栏按预期工作。

此设置的唯一小问题是 Xcode 无法正确报告从属文件中的行错误,它只是指向包含并保留在那里。现在,我必须右键单击行错误并选择在日志中显示,这会在正确的行号所在位置播撒完整的控制台消息。

如果您不编写糟糕的代码(呵呵)或使用外部文本编辑器,这并不是一个大问题,但如果您习惯使用快速修复键从一个错误中跳转,那么这会严重伤害您线到另一个。

通过这个技巧,我将一个 1151 行文件分成四个大小分别为 558、342、55 和 145 行的文件。现在,功能的相关性更好,并且代码仍然可以作为同一类的一部分工作,因此我不需要像使用类或接口等语言构造那样编写访问器。

In the end I have resorted to the basic preprocessor, meaning includes and some trickery. Here's what I've done:

  1. Given a table view controller filename.m, create one filename_functionality.m file for each tab/group of related functionality and put there the methods. Note that there is no @implementation, @end, #include, etc, in this file, just the methods.
  2. In your Xcode project add the new files, but be careful to not mark them as required for any build targets. If you forget, select later the files and uncheck the mark which includes them into your target. This is to avoid Xcode trying to compile these rouge files alone.
  3. At the end of your main filename.m and just before your @end marker, add the necessary #include "filename_functionality.m", which effectively tells the preprocessor to treat all the files as a single one.
  4. For the cases where your main file is calling methods of your subordinate files, add to the top of your main file an anonymous interface and declare there the prototypes of the methods you moved to the other files.

At this point, it works! However, Xcode is still really annoying, despite being able to open the subordinate source code files, it doesn't parse them properly, so you can't use the quick navigation bar to jump between methods, for example. Again, to solve this more preprocessor trickery:

  1. At the beginning of each subordinate file, add (yuck, wiki formatting is broken):

    #ifndef _INCLUDE_FILES

    @implementation BIEntity_view_controller

    #endif

  2. At the end of your subordinate file repeat with ifndef enclosing an @end.

  3. At the end of your main file and before the includes, define _INCLUDE_FILES.

What this does is trick XCode into thinking that this is a proper implementation file, so syntax highlighting, completion and navigation bars work as expected.

The only minor nit with this setup is that Xcode doesn't properly report errors for lines in your subordinate files, it just points to the include and stays there. Now I have to right click on the line error and select Reveal in Log which sows the full console message where the correct line numbers are.

This is not a big problem if you don't write bad code (hehe) or use an external text editor anyway, but will seriously hurt if you are used to the quick fix keys to jump from one error line to another.

With this trick I've split a 1151 line file into four of sizes 558, 342, 55 and 145 lines each. Now the functionality is better related and the code can still work as part of the same class, so I don't need to write accessors as would be the case if using a language construct like classes or interfaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文