简体   繁体   中英

Calling a method from other class

I have 2 classes named as:

  • InputFunctionView
  • BaseTemplateView

I created a method cancelStickerPreviewButtonPressed inside InputFunctionView class that I want to call it from BaseTemplateView class.

Here is my code:

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];

}

My question is why in InputFunctionView.m method cancelStickerPreviewButtonPressed this below part doesn't work? previewCancelButton and stickerPreviewView are supposed to be removed from its superview but no. What am I missing?

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

Might be help you using Class method

In InputFunctionView.h

+(void)yourMethod;

In InputFunctionView.m

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

In BaseTemplateView.h

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

     [InputFunctionView yourMethod];
}

Happy coding...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM