0

I am test the code, I build two uiviewcontroller ,A , B and use navigation .

When I click A Button , it will navigate to B UIviewController.

But I need click the back button on the navigation bar , it will pass a value return to A uiviewcontroller.

But it was not find the selector when I click back button. The log still show 0.

My AViewController header is below:

    #import <UIKit/UIKit.h>
    #import "BViewController.h"

    @interface AViewController : UIViewController<BViewControllerDelegate>

    -(void) bViewController:(BViewController *)childViewController sendBackPassValue:(NSString *)value;

    @end

AViewController implement file is below:

    #import "BViewController.h"
    ...
    -(void) bViewController:(BViewController *)childViewController sendBackPassValue:(NSString *)value
    {

        NSLog(@"Back Value: %@", value );
    }
    ...

B View Controller header file is below:

    #import <UIKit/UIKit.h>
    @class BViewController;

    @protocol BViewControllerDelegate <NSObject>

    -(void) bViewController:(BViewController*) childViewController sendBackPassValue:(NSString*) value;

    end


    @interface BViewController : UIViewController

    @property (strong,nonatomic) NSString *passValue;
    @property (weak, nonatomic) id<BViewControllerDelegate> delegate;

    @end

B View controller implement file is below: #import "BViewController.h" #import "AViewController.h"

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

        _passValue = @"my pass value";

        UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Home"         style:UIBarButtonItemStyleBordered target:self action:@selector(home:)];
        self.navigationItem.leftBarButtonItem=newBackButton;
    }

    -(void)home:(UIBarButtonItem *)sender {

        NSLog(@"home button click");
        NSLog(@"[_delegate respondsToSelector:@selector(bViewController:sendBackDateTime:)]:%d",[_delegate respondsToSelector:@selector(bViewController:sendBackPassValue:)]);

        if( [_delegate respondsToSelector:@selector(bViewController:sendBackPassValue:)])
        {
            NSLog(@"sendBackPassValue");
            [_delegate bViewController:self sendBackPassValue:_passValue];
        }

        [self.navigationController popToRootViewControllerAnimated:YES];

    }
    ...

But the

    NSLog(@"[_delegate respondsToSelector:@selector(bViewController:sendBackDateTime:)]:%d",[_delegate respondsToSelector:@selector(bViewController:sendBackPassValue:)]);

still get 0.

Have any one know what step I was less?

Thank you very much.

--answer--

add this code in AUIViewcontroller.m

it can trigger the delegate.

    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        BViewController *nextVC = segue.destinationViewController;
        nextVC.delegate = self;
    }
dickfala
  • 3,246
  • 3
  • 31
  • 52
  • 1
    When A creates B, do you set the `delegate` property? Post the code where A creates and displays B. – rmaddy Aug 05 '14 at 02:38
  • sorry , I forget commit the code. I had renew the git. thank you – dickfala Aug 05 '14 at 08:09
  • You didn't answer my question. Do you set the `delegate` property? Update your question with the code that creates and displays the B controller. Don't make people access your code elsewhere. It belongs in the question. – rmaddy Aug 05 '14 at 17:47
  • Thanks rmaddy. I resolve the problem. Set A Viewcontroller delegate, It can show the log value 1. – dickfala Aug 06 '14 at 01:07

2 Answers2

0

Try this:

-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
   // back button was pressed.  We know this is true because self is no longer
   // in the navigation stack.  
}
[super viewWillDisappear:animated];

}

Source: Setting action for back button in navigation controller

Community
  • 1
  • 1
scollaco
  • 947
  • 8
  • 13
  • What does this have to do with the question? And FTI - there are much better ways to see if a view controller is being dismissed. – rmaddy Aug 05 '14 at 02:39
  • This conditional confirms if the user pressed back button, so there he can pass the value to the A controller. – scollaco Aug 05 '14 at 11:23
  • Your answer is not what the question is about. The OP has their own button. The problem is with calling the delegate method. Read the code in the question. – rmaddy Aug 05 '14 at 17:49
0

add this code in AUIViewcontroller.m

it can trigger the delegate.

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    BViewController *nextVC = segue.destinationViewController;
    nextVC.delegate = self;
}
dickfala
  • 3,246
  • 3
  • 31
  • 52