我在这个网站上发现了类似的问题,但没有一个以清晰和基本的方式解决问题的问题。
我有我的 ReadViewController.h 和 ReadViewController.m 文件以及我的 ChooseViewController.h 和 ChooseViewController.m 文件。
他们都需要访问当前在 ReadViewController.m 文件中的 getProperties 方法。
- (void) getProperties {
NSLog(@"Start getProperties");
//SOME CODE
NSLog(@"End getProperties");
}
现在理想情况下,这将在名为 GeneralModel.m 的第三个文件中
请给我一个基本示例,说明 Controller 文件中需要包含哪些代码才能调用此方法。
最佳答案
如果此方法将在应用程序中的许多地方使用,那么在这种情况下,您应该将其视为全局方法并尝试将此方法放在单独的类中,可能是 NSObject
类的类型。
@interface Utility :NSobject
- (void) getProperties
@end
@implementation Utility
- (void) getProperties {
NSLog(@"Start getProperties");
//SOME CODE
NSLog(@"End getProperties");
}
@end
在这里,只要你需要这些方法,你只需要创建Utility
类的对象就可以在任何需要的地方轻松访问它。就像
在ReadViewController
中就是这样创建对象和访问
Utility * obje = [Utility alloc]init];
[obje getProperties ];
有一件事,如果你只是谈论应用程序架构,假设你遵循 MVC
在这种情况下你应该保留你的 model(NSObject Type)Class
来制作一些数据库调用,请求调用服务器。单独保留 View
类代码,如 UIView
,并将代码放在 Controller 类
中,只需要控制 App 的逻辑。
Here is the Link which explain The MVC
Architecture.
希望你能明白。
关于iOS调用另一个类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13360705/