从其他课程

时间:2016-09-23 08:47:38

标签: ios objective-c methods

我有两个名为:

的班级
  • InputFunctionView
  • BaseTemplateView

我在 InputFunctionView 类中创建了一个方法 cancelStickerPreviewButtonPressed ,我想从 BaseTemplateView 类中调用它。

这是我的代码:

InputFunctionView.h

#import <...>

@class InputFunctionView;
@protocol InputFunctionViewDelegate <NSObject>
 ...
@end

@interface InputFunctionView : UIView <...> {

}


- (void)cancelStickerPreviewButtonPressed:(id)sender;

@end

InputFunctionView.m

- (void)cancelStickerPreviewButtonPressed:(id)sender {

    // This part doesn't work when called from other class. Why?
    NSLog(@"cancel sticker preview");
    [self.previewCancelButton removeFromSuperview];
    [_stickerPreviewView removeFromSuperview];

}

BaseTemplateView.h

    #import <...>

    @interface BaseTemplateView : UIViewController


    @end

BaseTemplateView.m

#import "InputFunctionView.h"

- (void) MethodA {

InputFunctionView *IFV = [[InputFunctionView alloc]init];
[IFV cancelStickerPreviewButtonPressed:nil];

}

我的问题是为什么在 InputFunctionView.m 方法 cancelStickerPreviewButtonPressed 下面这个部分不起作用?应该从其superview中删除previewCancelButton和stickerPreviewView但不是。我错过了什么?

// This part doesn't work when called from other class. Why?
    NSLog(@"cancel sticker preview");
    [self.previewCancelButton removeFromSuperview];
    [_stickerPreviewView removeFromSuperview];

1 个答案:

答案 0 :(得分:0)

可以帮助您使用Class方法

InputFunctionView.h

+(void)yourMethod;

InputFunctionView.m

+(void)yourMethod
{
    NSLog(@"Your method called");
}

BaseTemplateView.h

- (void) MethodA {  // Need to import InputFunctionView.h 

     [InputFunctionView yourMethod];
}

快乐的编码......