问题描述
我开发了一些应用程序,直到现在。现在我正在写一个新的,在这个项目中,我想保持代码非常干净,所以很容易找到方法。
我想从 UIViewControllers 其视图具有 UITableView 作为子视图。我希望有一个文件名为 DetailViewController
的所有直接属于它的函数。另一个名为 DetailViewController + Protocols
的文件应该包含上面的类别和所有这些委托方法 UITableView 。
是否可以这样做?
EDIT p>DetailViewController.h
@interface DetailViewController:UIViewController
...一些属性
...一些方法
@end
DetailViewController.m
#importDetailViewController.h
@implementation DetailViewController
...一些合成
...一些方法
@end
DetailViewController + Protocols.h
@ interface DetailViewController(Protocols)< UITableViewDelegate,UITableViewDataSource>
@end
DetailViewController + Protocols.m
@implementation DetailViewController(Protocols)
- (NSINteger)numberOfSections
{
return ... ;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
return ...;
return ...;
}
...
@end
但是,然后Xcode显示一个警告,一些委托方法没有实现在DetailViewController。我也尝试导入 DetailViewController + Protocols.h 在 DetailViewController.h 。没有变化。
然后我试过忽略警告,看到:它工作!但为什么?难道不应该在没有这些警告的情况下工作吗?
唯一要记住的类别是,你不能 @synthesize
中的属性。
请参阅了解详情。
I developed some application 'till now. Now I'm writing a new one and in this project I want to keep the code very clean, so it's very easy to find the methods.
I want to start with the UIViewControllers whose view have a UITableView as subview. I wish to have a file with the name DetailViewController
for all functions which belong directly to it. Another file with the name DetailViewController+Protocols
should contain a category of the class above and all these delegate methods of UITableView.
Is it possible to do something like this? I want to keep my code clean and split it into multiple files.
EDIT
DetailViewController.h
@interface DetailViewController : UIViewController
... Some Properties
... Some Methods
@end
DetailViewController.m
#import "DetailViewController.h"
@implementation DetailViewController
... Some Synthesizes
... Some Methods
@end
DetailViewController+Protocols.h
@interface DetailViewController (Protocols) <UITableViewDelegate, UITableViewDataSource>
@end
DetailViewController+Protocols.m
@implementation DetailViewController (Protocols)
- (NSINteger)numberOfSections
{
return ...;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return ...;
return ...;
}
...
@end
But then Xcode shows a warning that some of the delegate methods are not implemented in DetailViewController. I also tried it with importing the DetailViewController+Protocols.h in the DetailViewController.h. No changing.
Then I tried it with ignoring the warnings and see: It worked! But why? Shouldn't it work without these warnings?
Yes it is. The only thing to keep in mind with categories is that you cannot @synthesize
properties in them. Adding ivars in categories is more difficult as well.
See this for more info on that.
这篇关于将委托方法放入类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!