【问题标题】:Warning "Method definition not found" in Objective-C. Call Method from Category file在 Objective-C 中警告“找不到方法定义”。从类别文件调用方法
【发布时间】:2016-08-24 05:54:06
【问题描述】:

我在类别文件中创建了一些方法。我只想在我的视图控制器中重用这些方法。所以我在视图控制器中导入了该类别文件,并在头文件中声明了该方法。 像这样称呼它:

类别类:

@interface UIViewController (headerView)
-(UILabel *)someMethod;

@implementation UIViewController (headerView)
-(UILabel *)someMethod{
}

HomeViewController:

@interface HomeViewController : UIViewController
-(UILabel *)someMethod;

@implementation HomeViewController
[self someMethod];

我在这一行收到警告消息:

@implementation HomeViewController

它正在工作。但我想清除这个警告。我该怎么做?

【问题讨论】:

  • “在我的视图控制器中重用这些方法”是什么意思?类别将方法添加到现有类。调用这些方法的唯一问题是让 Clang 知道它们的定义。听起来你错过了一步。或许贴出分类界面并展示你如何导入它。
  • 你收到什么警告信息?
  • viewcontroller 中显示您如何创建类别以及如何将其导入的所有代码!

标签: ios objective-c xcode objective-c-category


【解决方案1】:

如果您希望在视图控制器中进行分类,请执行类似操作

你的类别

@interface UIViewController (ExtendedMethods)
- (void)someMethod;
@end

@implementation UIViewController (ExtendedMethods)
- (void)someMethod {
  NSLog(@"Some method");
}
@end

MyViewController.m

#import "MyViewController.h"
#import "UIViewController+ExtendedMethods.h"

@implementation MyViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  [self someMethod];
}
@end

【讨论】:

  • 是的。正是我这样做过。但收到警告信息。如何清除?
  • 您是否收到有关此行的任何警告? - 实现 MyViewController
  • 不,我没有收到任何警告。你确定你在类别头文件中有一些方法吗?也许你拼错了?
  • 还要确保在文件中导入#import "UIViewController+ExtendedMethods.h" 来尝试从类别中使用这些方法
  • 是的。我再次在 MyViewController 头文件中声明了 someMethod。这里的错误。谢了哥们。感谢您的帮助!
猜你喜欢
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多