[英]What's a good way to split a tabbed UITableViewController into multiple files?
I've implemented a tabbed UITableViewController
. 我实现了一个选项卡式
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.
一切都很好,但是我最终得到一个包含3个不同实现的源文件,即使使用编译指示标记源代码的各个部分,也变得有点笨拙和混乱。
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. 我曾考虑过在运行时根据选定的选项卡从字符串创建选择器,然后将
.m
文件拆分为几个,放置重命名的方法,但是然后出现强制@end
和文件结尾,编译器告诉您缺少要实施的方法。
Really, it looks like objective-c wasn't designed to split the source though several files. 确实,看起来Objective-C并非旨在通过几个文件来拆分源。 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. 我以某种方式设法在主类的
@end
之前使用#include <otherfile.m>
来模拟所有这些,但是看起来并不漂亮。 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). 另外,如果我尝试将文件包含在项目中,Xcode会感到困惑,因为它试图单独编译文件(至少我可以将文件包含在项目中,并禁止将它们包含在目标中)。
In the end I have resorted to the basic preprocessor, meaning includes and some trickery. 最后,我诉诸于基本的预处理程序,这意味着包括一些技巧。 Here's what I've done:
这是我所做的:
filename.m
, create one filename_functionality.m
file for each tab/group of related functionality and put there the methods. filename.m
,为每个与功能相关的选项卡/组创建一个filename_functionality.m
文件,并将方法放在那里。 Note that there is no @implementation
, @end
, #include
, etc, in this file, just the methods. @implementation
, @end
, #include
,等等,在这个文件中,只是方法。 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. filename.m
的末尾和@end
标记之前,添加必要的#include "filename_functionality.m"
,它有效地告诉预处理器将所有文件都视为一个文件。 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.
但是,Xcode仍然很烦人,尽管能够打开从属源代码文件,但它无法正确解析它们,因此,例如,您不能使用快速导航栏在方法之间跳转。 Again, to solve this more preprocessor trickery:
同样,要解决此更多的预处理器难题:
At the beginning of each subordinate file, add (yuck, wiki formatting is broken): 在每个下级文件的开头,添加(糟糕,Wiki格式已损坏):
#ifndef _INCLUDE_FILES
@implementation BIEntity_view_controller
#endif
At the end of your subordinate file repeat with ifndef
enclosing an @end
. 在下级文件的末尾,重复
ifndef
在@end
加上@end
。
_INCLUDE_FILES
. _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. 这样做是欺骗XCode认为这是一个正确的实现文件,因此语法突出显示,完成和导航栏可以按预期工作。
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. 此设置的唯一缺点是Xcode不会正确报告下级文件中的行错误,它仅指向包含并停留在该位置。 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. 如果您不编写任何错误代码(hehe)或不使用外部文本编辑器,这不是一个大问题,但是如果习惯于使用快速修复键从一个错误行跳到另一个错误行,则会造成严重伤害。
With this trick I've split a 1151 line file into four of sizes 558, 342, 55 and 145 lines each. 通过这个技巧,我将1151行文件拆分为四个大小分别为558、342、55和145行的文件。 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.
现在,功能之间的关联性更好,并且代码仍可以作为同一类的一部分工作,因此,与使用类或接口之类的语言构造一样,我不需要编写访问器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.