Question

I have this delegate that is not working out as planned, I have it setup like so. I want to call the function NSLog(show); I am not too sure why this isn't work, but works with my other viewcontroller. I must be missing some small detail.

AccountViewController.h

@protocol AccountViewControllerDelegate;
@interface AccountViewController : UIViewController{

}
@property (nonatomic, assign) id <AccountViewControllerDelegate> accountViewDelegate;
@end

@protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
@end

AccountViewController.m

-(IBAction)save:(id)sender {
    [self showLabel];
}

- (void)showLabel {
    if (self.accountViewDelegate) {
        NSLog(@"showlabel");

        [self.accountViewDelegate showLabel];
    }
}

MapViewController.m

-(void)showLabel {
    NSLog(@"SHOW");
}
Was it helpful?

Solution 2

Kindly note that delegate should not have strong reference.

So use

@property (unsafe_unretained) id <AccountViewControllerDelegate> accountViewDelegate;

Now in MapViewController.m or MapViewController.h conform to this protocol as

@interface MapViewController : UIViewController <AccountViewControllerDelegate>{

}

Then in MapViewController.m please do

AccountViewController *accountVC = [[AccountViewController alloc]init]; // initialize it with whatever be like storyboard or nib
accountVC.accountViewDelegate = self;

Your AccountViewController.h file should look like this

@protocol AccountViewControllerDelegate <NSObject>
 - (void)showLabel;
@end

@interface AccountViewController : UIViewController{

}
@property (unsafe_unretained) id <AccountViewControllerDelegate> accountViewDelegate;
@end

OTHER TIPS

you haven't shown where you assign the MapViewController to be the delegate of the AccountViewController. Perhaps thats what you are missing

//(from somewhere in the MapViewController)

AccountViewController *accountVC = //however you instantiate it (segue, storyboard etc
accountVC.accountViewDelegate = self;

Here what you should have in your different files:

AccountViewController.h

Note that "assign" is mainly for primitives like NSInteger, CGFloat, BOOL (that is, for properties that are not objects). Use "weak" to keep a pointer toward your delegate without incrementing his retain count.

@protocol AccountViewControllerDelegate;

@interface AccountViewController : UIViewController
@property (nonatomic, weak) id <AccountViewControllerDelegate> accountViewDelegate;
@end

@protocol AccountViewControllerDelegate <NSObject>
- (void)showLabel;
@end

AccountViewController.m

Here, it is usually good practice to check if the delegate isn't nil and if it has the method you want to call implemented. Use respondToSelector for that.

-(IBAction)save:(id)sender {
    [self showLabel];
}

- (void)showLabel {
    if (self.accountViewDelegate && [self.accountViewDelegate respondsToSelector:@selector(showLabel)]) {
        NSLog(@"showlabel");

        [self.accountViewDelegate showLabel];
    }
}

MapViewController.h

Be sure to import "AccountViewController.h" here.

#import "AccountViewController.h"
@interface MapViewController : UIViewController <AccountViewControllerDelegate>
@end

MapViewController.m

Put the following where you were instantiating your AccountViewController object:

//Somewhere
AccountViewController *accountViewController = [[AccountViewController alloc] init];
accountViewController.accountViewDelegate = self;

and this is your delegate's method implementation:

- (void)showLabel {
    NSLog(@"SHOW");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top