Calling a method from other class - ios

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...

Related

Ios:Delegete method does not called

I'm trying to call a delegate method but isn't working. Where is my mistake?
Here is the class where i have declared #protocol, delegate and the method:
#import "EEBaseVC.h"
#protocol EEInterstitialScreenDelegete;
#interface EEInterstitialScreen : EEBaseVC
#property(nonatomic,strong) id <EEInterstitialScreenDelegete> delegete;
#end
#protocol EEInterstitialScreenDelegete <NSObject>
-(void)interstitialScreenUpdateInfo:(EEInterstitialScreen *)interstitialscreen;
#end
And this is the declaration of the delegate method in another ViewController:
#pragma mark - EEInterstitialScreenDelegate
-(void)interstitialScreenUpdateInfo:(EEInterstitialScreen *)interstitialscreen
{
NSLog("hello");
}
I have to do some stuff to call this method in the previous view controller. But the if condition is not satisfied:
if (_delegete && [_delegete respondsToSelector:#selector(interstitialScreenUpdateInfo:)] ) {
[_delegete interstitialScreenUpdateInfo:self];
}
AnotherViewController * obj = [AnotherViewController alloc]init];
obj.delegate =self;
[self.navigationController pushViewController:obj];

Delegate Pattern ios

I've been following this example to help me build a delegate but unfortunately I've missed something so it is not working for me. How do I set up a simple delegate to communicate between two view controllers?
My code looks like this:
// HintsViewController.h
#import <UIKit/UIKit.h>
#protocol HintDelegateProtocol;
#interface HintsViewController : UIViewController
#property (weak, nonatomic) id<HintDelegateProtocol> hintDelegate;
-(IBAction)showFirstLetter:(id)sender
-(IBAction)showHint:(id)sender;
-(IBAction)showAnswer:(id)sender;
#end
#protocol HintDelegateProtocol <NSObject>
-(void)HintsViewController:(HintsViewController*)hintsViewController
showFirstLetter:(NSString*)firstLetter;
-(void)HintsViewController:(HintsViewController*)hintsViewController
showHint:(NSString*)hint;
-(void)HintsViewController:(HintsViewController*)hintsViewController
showAnswer:(NSString*)answer;
#end
//
// HintsViewController.m
#import "HintsViewController.h"
#implementation HintsViewController
#pragma mark -
#pragma mark IBActions
/* As per a suggestion below I changed the code here /*
- (IBAction)showHint:(id)sender
{
[self.hintDelegate HintsViewController:self showHint:#"Hint"];
}
- (IBAction)showFirstLetter:(id)sender
{
[self.hintDelegate HintsViewController:self showFirstLetter:#"FirstLetter"];
}
- (IBAction)showAnswer:(id)sender
{
[self.hintDelegate HintsViewController:self showAnswer:#"Answer"];
}
#end
And then in the a Controller class I have the following:
//
// GameLogicController.h
#import "HintsViewController.h"
#interface GameLogicController : NSObject < HintDelegateProtocol>
#end
And in the implementation I have the following:
// GameLogicController.m
-(void) nextRiddle
{
HintsViewController *hintsViewController = [[HintsViewController alloc] init];
hintsViewController.hintDelegate = self;
}
#pragma mark -
#pragma mark HintsFunctionality
-(void)HintsViewController:(HintsViewController*)hintsViewController
showFirstLetter:(NSString*)firstLetter
{
NSLog(#"Show First Letter called");
}
-(void)HintsViewController:(HintsViewController*)hintsViewController
showHint:(NSString*)hint
{
NSLog(#"show Hint called");
}
-(void)HintsViewController:(HintsViewController*)hintsViewController
showAnswer:(NSString*)answer
{
NSLog(#"Show answer called");
}
Using breakpoints I can see that the IBActions in the HintsViewController are being called, but putting a breakpoint in any of the delegate methods in the gameLogicController are never hit. So I have missed an important step in setting up the connection between the GameLogicController and the HintsViewController. Can anyone help me spot it?
Say you have two files: one is your ViewController, and other is your ConnectionManager Class.
Declare protocol and its methods in your ConnectionManager class, and define your protocol methods in the ViewController class. By setting the delegate of your ConnectionManager class in ViewController Class, you can call your Protocol method.
#protocol ConnManagerDelegate<NSObject>
- (void)didReceiveData:(NSDictionary *)data;
- (void)didFailWithError:(NSError*)error;
#end
#interface ConnectionManager : NSObject<NSURLConnectionDelegate>
#property(nonatomic,assign)id< ConnManagerDelegate > delegate;
And elseswhere in the same file .m, when your response comes just call
[Self.delegate didReceiveData:mDict];
In the ViewController file after you alloc init ConnectionManager class, set its delegate to self and define the protocol methods. It is these methods you will have your response from ConnectionManager class.
This is all Protocol Delegation pattern

Subclass as delegate of superclass

I have class ImageViewController. It has delegate:
#protocol ImageViewControllerDelegate
#optional
- (void)singleTapGestureRecognizer:(UITapGestureRecognizer *)gesture;
- (void)imageDidLoaded;
I also have class AttachmentViewController that subclass of ImageViewController. In that class I want to get event then image property in changed. So here is my code of it change:
- (void)setImage:(UIImage *)image
{
// * Assign image with animation
[UIView transitionWithView:self.imageView
duration:k_DURATION_imageAppearence
options:UIViewAnimationOptionTransitionCrossDissolve
animations: ^{
self.imageView.alpha = 1;
} completion:^(BOOL finished) {
if ([self respondsToSelector:#selector(imageDidLoaded)]) {
[self.delegate imageDidLoaded];
}
}];
But I can not use
if ([self.DELEGATE respondsToSelector:#selector(imageDidLoaded)])
Then I do it I have error:
No known instance method for selector 'respondsToSelector:'
Why? And how here I need to use this capabilities? Is my implementation ok? Or how can I get this notification?
I think that here will be ok to create clear methods in superclass and override it in subclass if it needs to implement is. Is it best way?
You should declare your protocol as #protocol ImageViewControllerDelegate <NSObject>
This says that any object that conforms to your protocol will also conform to the NSObject protocol that respondsToSelector: is declared in.
There's really not enough code here to understand what you're trying to do. Generally to setup a delegate you have a weak property on your class that represents the delegate, and a parent to that class's instance would set the delegate.
Here's some pseudo code:
#protocol SomeDelegateProtocol<NSObject>
- (void)someMethod:(id)someObject;
#end
#interface SomeClass:NSObject
#property (nonatomic, weak) id<SomeDelegateProtocol>delegate;
#end
#implementation SomeClass
- (void)someFunction {
if ([self.delegate respondsToSelector:#selector(someMethod:)]) {
// do code stuff
}
}
#end
///////////
#implementation SomeParentClass
- (void)someOtherFunction {
SomeClass *instance = [SomeClass new];
instance.delegate = self; // assuming self implements SomeDelegateProtocol, otherwise you get a warning
}
Hope this helps!

Overriding class methods in ObjectiveC

Considering the following case:
#interface Superclass : NSObject
...
#end
#interface Superclass
+ (void)methodToOverride
{
NSLog(#"This method should be overridden by subclass!");
}
+ (void)callMethodToOverride
{
[self methodToOverride];
}
#end
#interface SubClass : SuperClass
...
#end
#implementation SubClass
+ (void)methodToOverride
{
NSLog(#"I'm overriding this method!");
}
#end
Now, when calling
[Subclass callMethodToOverride];
I get "This method should be overridden by subclass!". Is it possible to get I'm overriding this method! instead or is this not possible with ObjC's static methods?
Cheers!
I use this paradigm from time to time and it works for me. In my implementations, I'm referencing [self class] instead of self. Maybe that's the key you're missing.
+ (void)callMethodToOverride
{
[[self class] methodToOverride];
NSLog(#"This is the class that I just used: %#", NSStringFromClass(self));
}
Sounds like it could just be a typo where you're not using the class you think you are.

Why is my method not being called?

Why is my method not being called?
My UIViewController should be calling a method in my UIView called myMethod.
It only works on the inital UIView viewDidLoad.
After the view is loaded, I can't call the "myMethod" from someOtherMethod. And I don't understand why? XCode recognizes that the method exists and the method is exposed in my header.
MyViewController.h
#import “MyView.h”
#interface MyViewController : UIViewController {
MyView *mv;
}
MyViewContoller.m
#import “MyView.h”
- (void)viewDidLoad
{
mv = [[MyView alloc] initWithFrame:self.view.frame];
[self.view addSubview:mv];
//THIS WORKS IF CALLED FROM viewDidLoad
[mv myMethod];
}
- (void) someOtherMethod {
//THIS DOESN’T WORK IF CALLED LATER
[mv myMethod];
}
MyView.h
- (void) myMethod;
MyView.m
- (void) myMethod {
NSLog(#"My Method");
}

Resources