我有2個視圖控制器,PerksDetailsViewController
和BarcodeViewController
。我使用代表團將一個Card
類和從PerksDetailsVC中獲得的點數傳遞給BarcodeVC。解決編譯器中的警告:發送'BarCodeViewController * _strong'到不兼容類型的參數'id <PerksDetailsDelegate>'
我的應用程序的作品,但是我想擺脫這種警告: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
好的,我將'#import PerksDetailsViewController.h'和''從BarcodeVC.m移到BarcodeVC.h。然而,現在不僅僅是一個錯誤出現,而是一個錯誤說「找不到'PerksDetailsDelegate'的協議聲明」。 –
Grauzten
如果它在PerksDetailsViewController.h中聲明並且包含該文件,則說明它找不到它是沒有意義的。你確定這不是拼寫錯誤嗎?或者也許嘗試一個清潔? – Joel
不,它沒有拼錯。乾淨呢? – Grauzten