ios单击后退按钮传递值无法找到选择器

时间:2014-08-05 00:47:23

标签: ios xcode navigation

我正在测试代码,我构建了两个uiviewcontroller,A,B并使用导航。

当我点击A按钮时,它将导航到B UIviewController。

但我需要点击导航栏上的后退按钮,它会将值返回给uiviewcontroller。

但单击后退按钮时找不到选择器。日志仍然显示为0。

我的AViewController标题如下:

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

    @interface AViewController : UIViewController<BViewControllerDelegate>

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

    @end

AViewController实现文件如下:

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

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

B视图控制器头文件如下:

    #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视图控制器实现文件如下:         #import&#34; BViewController.h&#34;         #import&#34; AViewController.h&#34;

    ...
    - (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];

    }
    ...

但是

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

仍然是0。

有没有人知道我哪个步骤少了?

非常感谢。

- 应答 -

在AUIViewcontroller.m

中添加此代码

它可以触发委托。

    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        BViewController *nextVC = segue.destinationViewController;
        nextVC.delegate = self;
    }

2 个答案:

答案 0 :(得分:0)

试试这个:

-(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];

}

来源:Setting action for back button in navigation controller

答案 1 :(得分:0)

在AUIViewcontroller.m

中添加此代码

它可以触发代表。

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    BViewController *nextVC = segue.destinationViewController;
    nextVC.delegate = self;
}