【问题标题】:Two identical objective-c delegates - one works, the other does not两个相同的 Objective-c 代表 - 一个有效,另一个无效
【发布时间】:2015-02-15 18:53:54
【问题描述】:

我有一个“主屏幕”,其中包含两个用于计数的标签。我有两个不同的 UIViewController,它们是 HomeViewController 的代表。 ViewControllerA 调用该方法。

但是ViewControllerB的设置完全一样,除了命名之外,不会调用该方法。

我做错了什么?

HomeViewController.h

#import "ViewControllerA.h"
#import "ViewControllerB.h"

@interface HomeViewController : UIViewController <ViewControllerADelegate, ViewControllerBDelegate>

HomeViewController.m

- (void) updatedLabel {
    NSLog(@"Updating label count");
}

- (IBAction)btnA:(id)sender {
    [self performSegueWithIdentifier:@"segue_a" sender:self];
}

- (IBAction)btnB:(id)sender {

    [self performSegueWithIdentifier:@"segue_b" sender:self];

}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"segue_a"]) {
        ViewControllerA * vca = (ViewControllerA*) segue.destinationViewController;
         vca.delegate = self;
    }

    if ([[segue identifier] isEqualToString:@"segue_b"]) {
        ViewControllerB *vcb = (ViewControllerB*) segue.destinationViewController;
        vcb.delegate = self;
    }

}

ViewControllerA.h

@class ViewControllerA;

@protocol ViewControllerADelegate <NSObject>
- (void) updatedLabel;
@end

@interface ViewControllerA : UIViewController <UITextFieldDelegate>

@property (weak, nonatomic) id <ViewControllerADelegate> delegate;

ViewControllerA.m

- (IBAction)returnHome:(id)sender {
    [self.delegate updatedLabel];
    [self dismissViewControllerAnimated:YES completion:nil];
}

ViewControllerB.h

@class ViewControllerB;
@protocol ViewControllerBDelegate <NSObject>
- (void) updatedLabel;
@end

@interface ViewControllerB : UIViewController<AVCaptureMetadataOutputObjectsDelegate>

@property (weak, nonatomic)id <ViewControllerBDelegate> delegate;

ViewControllerB.m

- (IBAction)returnHome:(id)sender {
    [self.delegate updatedLabel];
    [self dismissViewControllerAnimated:YES completion:nil];
}

【问题讨论】:

  • 您是否尝试过调试您的代码?它在哪里失败?什么有效,什么无效?是否有任何意外值?
  • 我猜,在您执行第二次操作时,该视图控制器尚未创建,您正在对 nil 指针执行 setDelegate

标签: ios objective-c uiviewcontroller delegates


【解决方案1】:

很难说出了什么问题。是时候卷起袖子调试它了。在您的 prepareForSegue 中设置一个断点并单步执行,确保它在两个视图控制器类上都设置了委托。还要在两个视图控制器的 returnHome 方法上设置断点,并确保委托属性不为零。然后,当您到达委托调用时,单击“步入”按钮并观看它步入委托方法。

【讨论】:

  • 好的,我收回我原来的声明。经过进一步检查,ViewControllerB 为 nil。但我不明白为什么。
  • 我的 ViewControllerB 嵌入在故事板的导航控制器中。这有什么不同吗?
  • 您的 prepareForSegue 方法可能有问题。在此处设置断点并单步执行以查看会发生什么。
【解决方案2】:

可能是您在 HomeViewController 中实现 ViewControllerADelegate 和 ViewControllerBDelegate 协议。两种协议都有相同的方法 updatedLabel 并且这是模棱两可的?

编辑 1: 用调试器检查 vcb.delegate 不是 nil。

编辑 2: 如果您的 ViewControllerB 嵌入在 UINavigationController 中,则 segue.destinationViewController 将返回 UINavigationController 而不是 ViewControllerB

【讨论】:

  • 我也是这么想的。有一次,我做了一种不同的方法来测试,但它不起作用。
  • 不,没关系。你基本上承诺实现updatedLabel 两次。这里没有潜在的冲突。
【解决方案3】:

为遇到类似问题的人提供答案: ViewControllerA 没有嵌入到 NavigationController 中。 ViewControllerB 嵌入在 NavigationController 中。

用户 tgebarowski 在评论中指出,如果 ViewControllerB 嵌入在 UINavigationController 中,那么 segue.destinationViewController 将返回 UINavigationController 而不是 ViewControllerB。

因此,要解决此问题,如果您通过 Segue 创建委托,请使用以下内容。

    UINavigationController * nvc = (UINavigationController*)segue.destinationViewController;
    ViewControllerB *vcb = [nvc childViewControllers][0];
    vcb.delegate = self;

这就是它的全部内容。

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 2015-11-27
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多