0

私は2つのView Controllerを持っていPerksDetailsViewControllerますBarcodeViewController. 委任を使用して、特典のCardクラスとポイントを PerksDetailsVC から 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

BarcodeViewController.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 に答える