【问题标题】:Why can I not call a method from another class为什么我不能调用另一个类的方法
【发布时间】:2014-03-26 05:19:12
【问题描述】:

我有一个 NSObject 形式的自定义类(包)。我有很多这个对象的实例。在初始化每个实例时,我从 UIViewController 名称 ViewController 中传递 self。

在这些包的代码中,我想从 ViewController 调用一个方法。

-(void)toThisView:(UIView *)someView
{
    [imagesToRender addObject:someView];
    [self.mainImageView addSubview:someView];
}

在 Packages.m 中

我有

- (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 *)path andObject:(NSObject *)object

为什么会出现这个错误:

No visible @interface for 'UIViewController' declares the selector 'toThisView:'

【问题讨论】:

  • CustomClass *view2 = ...
  • YouCustomTomClassName 代替 UIViewController *view2 或者你可以使用执行选择器方法。

标签: ios objective-c uiviewcontroller


【解决方案1】:

如果 view2 是您的自定义类型对象,那么您可以简单地这样做:

[(YourCustomClass *)view2 toThisView:sender.view];

在视图控制器中导入YourCustomClass.

【讨论】:

  • 你是对的,它需要被转换为YourCustomClass。但我也遇到了问题,从未导入 YourCustomClass 的头文件
【解决方案2】:

听起来您有一个名为ViewController 的类,它是UIViewController 的子类。

听起来您也有一个名为 view2 的实例变量,您声明如下:

@implementation Packages {
    UIViewController *view2;
}

或者可能是这样的:

@interface Packages : NSObject

@property (nonatomic, strong) UIViewController *view2;

您需要将view2 声明为ViewController,而不是UIViewController。换句话说,像这样声明它:

@implementation Packages {
    ViewController *view2;
}

或者像这样:

@interface Packages : NSObject

@property (nonatomic, strong) ViewController *view2;

【讨论】:

    【解决方案3】:

    我认为你需要添加

    -(void)toThisView:(UIView *)someView
    

    到您的 viewController.h 文件,否则它不会在任何其他类中可见。

    【讨论】:

      【解决方案4】:

      首先在 viewController 中导入您的自定义类。 创建一个类的对象。

      调用方法就这么简单。

       [object toThisView:YourPrameter];
      

      在调用之前也要像这样在你的类.h文件中声明这个方法

      -(void)toThisView:(UIView *)someView;
      

      【讨论】:

        猜你喜欢
        • 2010-09-24
        • 2015-02-07
        • 2023-01-13
        • 1970-01-01
        • 1970-01-01
        • 2017-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多