[英]How to import another view controller header file when I use Objective-C category?
When I want to transition from one view controller to another, I import the second view controller's header file into my first view controller's header file, by writing #import "SecondViewController.h"
. 当我想从一个视图控制器转换到另一个视图控制器时,通过编写#import "SecondViewController.h"
,将第二个视图控制器的头文件导入到我的第一个视图控制器的头文件中。 However, since I already defined UIColor
category in my first view controller, when I try to import the second view controller, I enter the following error: Duplicate interface definition class for SecondViewController
. 但是,由于我已经在第一个视图控制器中定义了UIColor
类别,因此当我尝试导入第二个视图控制器时,我输入以下错误: Duplicate interface definition class for SecondViewController
。
Here's my FirstViewController.h
: 这是我的FirstViewController.h
:
#import
#import "SecondViewController.h"
@interface FirstViewController : UIViewController
@end
@interface UIColor (ColorWithInt)
+ (UIColor *)colorWithR:(CGFloat)red G:(CGFloat)green B:(CGFloat)blue A:(CGFloat)alpha;
@end
I didn't meet any such errors so far when I develop this app, so it's definitely this category that is causing the issue here. 到目前为止,当我开发此应用程序时,我还没有遇到任何此类错误,因此肯定是这一类引起了此问题。 So is it feasible to use category when I want to import another view controller class? 那么当我想导入另一个视图控制器类时使用类别是否可行? Or are there any alternative ways to extend UIColor
? 还是有扩展UIColor
其他方法? I just want to define a function that takes RGB as 0 ~ 255 integer, not 0 ~ 1 floating values that UIColor
uses on default. 我只想定义一个函数,将RGB作为0〜255的整数,而不是UIColor
默认使用的UIColor
浮点值。
I use iOS 7 and Xcode 5. 我使用iOS 7和Xcode 5。
您可能#import "SecondViewController.h"
两次,只需检查FirstViewController.h/m
文件(如果两者都这样做)。
I have a feeling you're using this 我有一种感觉,你正在使用这个
@interface
instead of 代替
@implementation
in your .m file. 在您的.m文件中。
I found out that the issue is not related to either FirstViewController
or SecondViewController
- let alone the category; 我发现问题与FirstViewController
或SecondViewController
-更不用说类别了。 it's because I imported almost all class' header file in AppDelegate.h
in order to initialize the relationship among UITabBarController
, UINavitationController
, RootViewController
, and Core Data and its lots of required properties. 这是因为为了初始化UITabBarController
, UINavitationController
, RootViewController
和Core Data及其大量必需属性之间的关系,我在AppDelegate.h
中导入了几乎所有类的头文件。 I didn't know that when I import a class in AppDelegate.h
I cannot import the class at some other class's header file. 我不知道在AppDelegate.h
导入类时,无法在其他类的头文件中导入该类。 Delete #import "FirstViewController.h";
删除#import "FirstViewController.h";
and #import "SecondViewController.h;"
和#import "SecondViewController.h;"
in AppDelegate.h
and I find my app being build properly now. 在AppDelegate.h
,我发现我的应用现在正在正确构建。 Thanks to those who left comments in this post. 感谢那些在这篇文章中发表评论的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.