我有2个视图控制器,PerksDetailsViewController
和BarcodeViewController
。我使用委托将PerksDetailsVC的Card
类和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
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
答案 0 :(得分:0)
在BarCodeViewController的.h实现中,您需要告诉编译器您实现了协议。
@interface BarCodeViewController : UIViewController <PerksDetailsDelegate> {
...
}