【问题标题】:Solving the warning in compiler: Sending 'BarCodeViewController *_strong' to parameter of incompatible type 'id<PerksDetailsDelegate>'解决编译器中的警告:将“BarCodeViewController *_strong”发送到不兼容类型“id<PerksDetailsDelegate>”的参数
【发布时间】:2012-05-11 03:58:14
【问题描述】:

我有 2 个视图控制器,PerksDetailsViewController 和 BarcodeViewController。我使用委托将 Card 类和 Perk 从 PerksDetailsVC 的特权点传递给 BarcodeVC。

我的应用可以运行,但是我想摆脱这个警告:Sending 'BarCodeViewController *_strong' to parameter of incompatible type 'id&lt;PerksDetailsDelegate&gt;'

来自[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

【问题讨论】:

    标签: iphone ios xcode delegates


    【解决方案1】:

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

    @interface BarCodeViewController : UIViewController <PerksDetailsDelegate> {
    ...
    }
    

    【讨论】:

    • 好的,所以我将 #import PerksDetailsViewController.h 和 &lt;PerksDetailsDelegate&gt; 从 BarcodeVC.m 移动到 BarcodeVC.h。然而,现在不仅出现了错误,而且出现了“找不到'PerksDetailsDelegate'的协议声明”的错误。
    • 如果它在 PerksDetailsViewController.h 中声明并且您已包含该文件,那么它说它找不到它是没有意义的。你确定不是拼写错误?或者尝试清洁?
    • 不,不是拼写错误。干净呢?
    • 尝试从你的协议定义中删除 。我不确定为什么会这样。然后你的协议应该在 PerksDetailsViewController.h 中定义,当你在 BarcodeViewController.h 中导入它时,它应该会找到它。
    • 删除 &lt;NSObject&gt; 似乎不会影响应用程序的任何不良情况。我在我的 BarcodeViewController.h 中导入了 PerksDetailsViewController.h 并做了与你原来的答案一样的事情。但是,我仍然收到我在此处的第一条评论中所述的相同错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    相关资源
    最近更新 更多