Ios 一个带有两个@interface的.h文件,并调用.m文件
在Ios 一个带有两个@interface的.h文件,并调用.m文件,ios,objective-c,uiview,uiviewcontroller,Ios,Objective C,Uiview,Uiviewcontroller,在.h文件中,我有如下两个界面: @interface Main : UIViewController @property (strong, nonatomic) IBOutlet UIView *MainView; @end @interface Sub : UIViewController @property (strong, nonatomic) IBOutlet UIView *testView; @end 之后,在.m文件中,我有这样的实现。如何在Main实现中调用testVie
.h
文件中,我有如下两个界面:
@interface Main : UIViewController
@property (strong, nonatomic) IBOutlet UIView *MainView;
@end
@interface Sub : UIViewController
@property (strong, nonatomic) IBOutlet UIView *testView;
@end
之后,在.m
文件中,我有这样的实现。如何在Main
实现中调用testView
(在不同的接口中声明)
@implementation Main
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
@end
已编辑-我不能拥有或不想拥有单独的.h和.m文件,因为我有很多这样的界面。恐怕我需要创建很多.h和.m文件。所以,我将所有这些合并到1.h和1.m文件中
你不能。因为
testView
封装在另一个类中,而这个类没有在Main
类中实例化。在一个.h和.m文件中分别有两个不同的类。您可以创建子类的实例,但也许您应该重新考虑您的类设计
顺便说一句,你为什么这么做?为什么不分成两个.h/.m文件 在主实现中定义一个子变量,您可以使用该变量的testView 但首先,写
@class Sub;
以前
@interface Main : UIViewController
.您还需要实现相同文件(.m文件)的子类 然后在主init方法中
@implementation Main
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
Sub * s1 = [Sub new];
// Now you can use s1.testView... and so on
}
return self;
}
@end
在Main中创建Sub的实例(Sub*aSub=…),然后可以访问testView,类似于:aSub.testView。但这些更改仅适用于“主”实例。谢谢。我会那样做的。谁反对这个问题?我可以知道为什么吗?我从来不会在没有给出任何理由的情况下否决某人,为什么你不能做一些像
Sub*Sub=[[Sub alloc]init];[次级设置视图:无]代码>或<代码>[子测试视图]或者他们可以创建一个普通的实例。当他们将文件的导入.h
到.m
文件时,这将包括Sub
的接口,因此他们需要做的就是Sub*Sub=[[Sub alloc]init]代码>做@class sub
是毫无意义的-嗨..我已经编辑了这个问题。我不能或不想分离.h和.m文件,因为我有许多界面,如图所示。
@implementation Main
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
Sub * s1 = [Sub new];
// Now you can use s1.testView... and so on
}
return self;
}
@end