简体   繁体   中英

unable to set delegate and call method

I created a delegate to pass data back to previous view controller after dismissing the current view controller.

this is how I am setting the delegate in

PaymentViewController.m

-(IBAction)techProcessAction:(id)sender
{
TechProcessPaymentVC *techVC = [[TechProcessPaymentVC alloc]init];
[techVC setTechDelegate:self];
 NSLog(@"DELEGATE == %@",techVC.techDelegate);

[self performSegueWithIdentifier:@"techProcess" sender:nil];
}

I can see that the delegate is set

Snap2Door[1765:1c703] DELEGATE == <PaymentDetailViewController: 0x9d3a360>

but when I check it in TechProcessPaymentVC 's viewDidLoad I'm getting DELEGATE == (null)

I guess this is the reason my callback method is not called which is in PaymentViewController.m .

this is how I defined the delegate in TechProcessPaymentVC.h

@protocol techProcessDelegate <NSObject>
-(void) techProcessViewControllerDismissed:(NSString *)paymentStatus;
@end

@interface TechProcessPaymentVC : UIViewController<UIWebViewDelegate>
{
id techDelegate;
}
@property (nonatomic, assign) id<techProcessDelegate>    techDelegate;

and this is how I am trying to call the method which is present in PaymentViewController.m

if ([[substrings objectAtIndex:0] isEqualToString:@"failure"]) {
        [self.techDelegate techProcessViewControllerDismissed:@"failure"];
        [self dismissViewControllerAnimated:YES completion:nil];
    }

Your problem is that you don't ever do anything with techVC . You allocate it, assign its delegate, then let it go out of scope (it'll be deallocated in ARC or leaked otherwise).

Try the following: First, remove the instantiation and assignment from techProcessAction: . Second, implement prepareForSegue:sender: and then assign self to be the techDelegate of the segue's destinationViewController . Your methods will look something like the following "code" (which I typed from memory and didn't try to compile):

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"techProcess"]) {
        ((TechProcessPaymentVC *)segue.destinationViewController).techDelegate = self;
    }
}

-(IBAction)techProcessAction:(id)sender
{
    [self performSegueWithIdentifier:@"techProcess" sender:nil];
}

viewDidLoad gets called when you init the TechProcessPaymentVC instance. Delegate gets set after that. Hence it is nil . Modify your init method to initWithDelegate and then assign the delegate and use it in viewDidLoad .

EDIT

Code:

TechProcessPaymentVC.h

@interface TechProcessPaymentVC : UIViewController

- (id) initWithDelegate:(id<techProcessDelegate>)delegate;

@end

TechProcessPaymentVC.m

#import "TechProcessPaymentVC.h"

@implementation TechProcessPaymentVC

- (id) initWithDelegate:(id<techProcessDelegate>)delegate {
    self = [super init];
    if(self) {
        self.techDelegate = delegate;
    }
    return self;
}

@end

Modify your call

-(IBAction)techProcessAction:(id)sender
{
    TechProcessPaymentVC *techVC = [[TechProcessPaymentVC alloc]initWithDelegate:self];
    //....
}

Hope that helps!

If your destination view controller is wrapped into a navigation controller, you have to refer to it differently in prepareForSegue:

UINavigationController *nav = segue.destinationViewController;
DetailViewController *dvc = [nav.viewControllers objectAtIndex:0];

In TechProcessPaymentVC.h:

@interface TechProcessPaymentVC : UIViewController<UIWebViewDelegate>
{
    id techDelegate_;
}
@property (nonatomic, assign) id<techProcessDelegate> techDelegate;
@end

In TechProcessPaymentVC.m:

@implementation TechProcessPaymentVC
@synthesize techDelegate = techDelegate_;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"%@", techDelegate_);
}
.
.
.
@end

Hope it's work for you!!!

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