简体   繁体   English

使用Objective-C类别时如何导入另一个视图控制器头文件?

[英]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文件中。

Self Answer 自我回答

I found out that the issue is not related to either FirstViewController or SecondViewController - let alone the category; 我发现问题与FirstViewControllerSecondViewController -更不用说类别了。 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. 这是因为为了初始化UITabBarControllerUINavitationControllerRootViewController和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.

相关问题 如何在另一个视图控制器中控制对象? (Xcode:objective-c) - How to control objects in another view controller? (Xcode:objective-c) 当我尝试在另一个视图控制器中访问数据时,为什么它为null? (Objective-C,XCode) - Why is it null when I try to access data in another view controller? (Objective-C, XCode) 当我尝试从另一个视图控制器(Objective-c?)访问变量时,为什么变量为null? - Why is my variable null when I am attempting to access it from another view controller (Objective-c? 如何在objective-c中导航到新故事板文件上的视图控制器 - How to navigate to a view controller on a new storyboard file in objective-c 如何在objective-c中识别模型,视图和控制器? - How do I identify a model, view and controller in objective-c? Objective-C以编程方式在另一个视图控制器上显示一个视图控制器 - Objective-C Showing a view controller on another view controller programmatically 在ios Objective-C项目中导入,使用和编译单个C ++头文件 - Import, use and compile a single c++ header file in an ios objective-c project 如果包含Project-Swift.h的导入,如何将Objective-C文件导入桥接头 - How to import Objective-C file to bridging header if it contains an import of Project-Swift.h Objective-C如何以编程方式导入文件? - Objective-c how to import file programmatically? 当在objective-c中点击pin图时移动到另一个视图控制器 - Move to another view controller when the pin map tapped in objective-c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM