0

我有 2 个视图控制器,PerksDetailsViewController并且BarcodeViewController. 我使用委托将CardPerksDetailsVC 中的一个类和一个 perk 点传递给 BarcodeVC。

我的应用程序有效,但我想摆脱这个警告:Sending 'BarCodeViewController *_strong' to parameter of incompatible type 'id<PerksDetailsDelegate>'

来自这条线[self setDelegate:barCodeVC];

方法里面

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showBarCode"]) {
        BarcodeViewController *barCodeVC = (BarcodeViewController *)segue.destinationViewController;

        [self setDelegate:barCodeVC];

    }
}

下面是一些可能有助于找到错误部分的代码

PerksDetailsViewController.m

#import Card.h
#import PerksDetailsViewController.h
#import BarcodeViewController.h

@interface PerksDetailsViewController () 

@end

@implementation
@synthesize delegate = _delegate;
@synthesize myCard = _myCard;

- (IBAction)redeemPressed:(id)sender {
    // get required points of a perk selected

     NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
     [f setNumberStyle:NSNumberFormatterDecimalStyle];

     self.pointsRequired = [f numberFromString: (self.pointsLabel.text)];

    NSLog(@"points required by the perk %@", self.pointsRequired);

    [self.delegate perksDetailsViewController:self didPassRequiredPoints:self.pointsRequired withCard:self.myCard];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showBarCode"]) {
        BarcodeViewController *barCodeVC = (BarcodeViewController *)segue.destinationViewController;

        [self setDelegate:barCodeVC];

    }
}

@end

PerksDetailsViewController.h

#import <UIKit/UIKit.h>
#import "Card.h"
#import "BarcodeViewController.h"

@class PerksDetailsViewController;

@protocol PerksDetailsDelegate <NSObject>

- (void)perksDetailsViewController:(PerksDetailsViewController *)sender
             didPassRequiredPoints: (NSNumber *) requiredPoints
                          withCard: (Card *) selectedCard;

@end

@interface PerksDetailsViewController : UIViewController 

@property (nonatomic, strong) id <PerksDetailsDelegate> delegate;

@property (nonatomic, strong) Card *myCard;

@end

条码视图控制器.m

#import "PerksDetailsViewController.h"

@interface BarcodeViewController () <PerksDetailsDelegate>
@end

@implementation BarcodeViewController
@synthesize myCard = _myCard;
@synthesize resultingPoints = _resultingPoints;

- (void)perksDetailsViewController:(PerksDetailsViewController *)sender didPassRequiredPoints:(NSNumber *)requiredPoints withCard:(Card *)selectedCard 
{

    double perksPoints = [requiredPoints doubleValue];

        self.myCard = selectedCard;
        self.resultingPoints = [NSNumber numberWithDouble:[selectedCard subtractPoints:perksPoints] ];
}

@end
4

1 回答 1

0

在 BarCodeViewController 的 .h 实现中,您需要告诉编译器您实现了协议。

@interface BarCodeViewController : UIViewController <PerksDetailsDelegate> {
...
}
于 2012-05-11T04:01:51.167 回答