Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 一个带有两个@interface的.h文件,并调用.m文件_Ios_Objective C_Uiview_Uiviewcontroller - Fatal编程技术网

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