Abhishek Mitra
Abhishek Mitra

Reputation: 3395

Protocol/Delegate is not working

I have three UIViewcontroller, namely ViewController, BViewController and CViewController, ViewController is RootViewController from UINavigationController,

I'm pushing from ViewController -> BViewController -> CViewController, through button action.

I have declared protocol in CViewController. This protocol method is working fine for BViewController, but I don't understand how to make work its delegate method in ViewController.

I had created an object of CViewController in View Controller, then declared self for that delegate, though its not working, Below is my code. Please help me. Where I'm doing wrong here !

My Root View Controller, namely ViewController

#import "ViewController.h"
#import "BViewController.h"
#import "CViewController.h"

@interface ViewController ()<testDelegates>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CViewController"];
    cVC.mydelegate = self;
}

- (IBAction)nextAction:(id)sender {

    BViewController *bViewC = [self.storyboard instantiateViewControllerWithIdentifier:@"BViewController"];

    [self.navigationController pushViewController:bViewC animated:YES];
}

-(void)TestMethod{

    NSLog(@" Its NOT Working");
}

My Second View Controller, Namely BViewController

#import "BViewController.h"
#import "CViewController.h"

@interface BViewController ()<testDelegates>

@end

@implementation BViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)bNextAction:(id)sender {

    CViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CViewController"];

//    cVC.mydelegate = self;
    [self.navigationController pushViewController:cVC animated:YES];
}

-(void)TestMethod{

    NSLog(@" Its Working If I uncomment to cVC.mydelegate = self");
}

And my third View Controller, Where i Declared a protocol in .h file, namely CViewController

#import <UIKit/UIKit.h>

@protocol testDelegates <NSObject>

-(void)TestMethod;

@end

@interface CViewController : UIViewController

@property (weak) id <testDelegates> mydelegate;

@end

And in .m file

#import "CViewController.h"

@interface CViewController ()

@end

@implementation CViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)cNextAction:(id)sender {

    [self.mydelegate TestMethod];
}

Upvotes: 1

Views: 121

Answers (4)

dip
dip

Reputation: 3598

If Your are trying to set Delegate of CViewController to ViewController then try putting

self.mydelegate = self.navigationController.viewControllers[0]

on viewDidLoad method of CViewController.

Else you are Trying to make ViewController And BViewController both be Delegate of CViewController then this is not possible like this, as Delegate of a Object Can only be unique. This means the delegate is simply a variable which holds reference of object and as you know value to a variable can be unique at a instance of time.

So If you are willing to do some task on both Viewcontroller and BViewController then use notification pattern. See apple doc here

Upvotes: 1

Sergey
Sergey

Reputation: 1617

You create class and this class destroying after you leave viewDidLoad method.

- (void)viewDidLoad {
    [super viewDidLoad];

    CViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CViewController"];
    cVC.mydelegate = self;
}

As I understand you need to perform some methods in the ViewController from the CViewController.

You can pass viewController as delegate from the BViewController and them past it to the CViewController.

Or you can use code like this:

- (IBAction)cNextAction:(id)sender {
    id< testDelegates > delegate = self.navigationController.viewControllers[0];
    if([delegate conformsToProtocol:@protocol(testDelegates)])
    {
        [delegate TestMethod];
    }
}

This is no best way but it's will be work.

Upvotes: 1

Kiran K
Kiran K

Reputation: 949

You need to declare a another Protocol into BViewController class.

Please follow below steps.

Step 1: BViewController.h

@protocol testDelegates2 <NSObject>

-(void)TestMethod2;

@interface BViewController : UIViewController

@property (weak) id <testDelegates> mydelegate;


@end
@implementation CViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)cNextAction:(id)sender {

    [self.mydelegate TestMethod2];
}

Step 2. In ViewController class conform Protocol
@interface ViewController ()<testDelegates>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CViewController *cVC = [self.storyboard instantiateViewControllerWithIdentifier:@"CViewController"];
    cVC.mydelegate = self;
}

- (IBAction)nextAction:(id)sender {

    BViewController *bViewC = [self.storyboard instantiateViewControllerWithIdentifier:@"BViewController"];
cVC.mydelegate = self; //important line
    [self.navigationController pushViewController:bViewC animated:YES];
}

-(void)TestMethod2{

    NSLog(@"Final Output");
}

Upvotes: 1

Basir Alam
Basir Alam

Reputation: 1358

Try this

self.delegate = self.navigationController.viewControllers[0];

Upvotes: 1

Related Questions