Ios 为什么我不能从另一个类调用方法
我有一个NSObject形式的自定义类(包)。我有很多这个对象的实例。在初始化这些实例时,我从UIViewController名称ViewController传递self 在这些包的代码中,我想从ViewController调用一个方法Ios 为什么我不能从另一个类调用方法,ios,objective-c,uiviewcontroller,Ios,Objective C,Uiviewcontroller,我有一个NSObject形式的自定义类(包)。我有很多这个对象的实例。在初始化这些实例时,我从UIViewController名称ViewController传递self 在这些包的代码中,我想从ViewController调用一个方法 -(void)toThisView:(UIView *)someView { [imagesToRender addObject:someView]; [self.mainImageView addSubview:someView]; } 而且
-(void)toThisView:(UIView *)someView
{
[imagesToRender addObject:someView];
[self.mainImageView addSubview:someView];
}
而且是打包的
我有
- (UIView *)handleTapFrom:(UITapGestureRecognizer *)sender
{
[view2 toThisView:sender.view]; // Error No visible @interface for 'UIViewController' declares the selector 'toThisView:'
}
其中,view2是UIViewController*view2
,并通过此类的init方法将其设置为view2=object
-(id)initWithPath:(NSString*)路径和对象:(NSObject*)对象
为什么我会出现这个错误:
“UIViewController”的
No visible@interface声明选择器“toThisView:”
听起来您有一个名为ViewController
的类,它是UIViewController
的子类
听起来您还有一个名为view2
的实例变量,声明如下:
@implementation Packages {
UIViewController *view2;
}
@interface Packages : NSObject
@property (nonatomic, strong) UIViewController *view2;
@implementation Packages {
ViewController *view2;
}
@interface Packages : NSObject
@property (nonatomic, strong) ViewController *view2;
或者可能是这样的:
@implementation Packages {
UIViewController *view2;
}
@interface Packages : NSObject
@property (nonatomic, strong) UIViewController *view2;
@implementation Packages {
ViewController *view2;
}
@interface Packages : NSObject
@property (nonatomic, strong) ViewController *view2;
您需要将view2
声明为ViewController
,而不是UIViewController
。换言之,声明如下:
@implementation Packages {
UIViewController *view2;
}
@interface Packages : NSObject
@property (nonatomic, strong) UIViewController *view2;
@implementation Packages {
ViewController *view2;
}
@interface Packages : NSObject
@property (nonatomic, strong) ViewController *view2;
或者像这样:
@implementation Packages {
UIViewController *view2;
}
@interface Packages : NSObject
@property (nonatomic, strong) UIViewController *view2;
@implementation Packages {
ViewController *view2;
}
@interface Packages : NSObject
@property (nonatomic, strong) ViewController *view2;
如果
view2
是您的自定义类型对象,则只需执行以下操作:
[(YourCustomClass*)视图2到该视图:sender.view]代码>
在viewController中导入自定义类。
。我认为您需要添加
-(void)toThisView:(UIView *)someView
到您的viewController.h文件,否则它将无法从任何其他类中看到。首先在viewController中导入自定义类。 创建类的对象 简单地像这样调用方法
[object toThisView:YourPrameter];
在调用之前,请在class.h文件中声明此方法,如下所示
-(void)toThisView:(UIView *)someView;
CustomClass*view2=…您可以使用CustomTomClassName而不是UIViewController*view2,或者您可以使用执行选择器方法。您是对的,它需要转换为您的CustomClass。但我也遇到了一个问题,从未为YourCustomClass导入头文件