当我尝试运行此代码时,我收到一条错误消息:
不兼容的整数到指针转换将'BOOL'(又名'signed char')发送到'BOOL *'类型的参数(又名'signed char *')
就行了
[self.MMdelegate addItemViewControllerMM:self didCheck1:checked1 didCheck2:checked2];
MatchModeViewController.h
@class MatchModeViewController;
@protocol MatchModeViewControllerDelegate <NSObject>
- (void)addItemViewControllerMM:(MatchModeViewController *)MMcontroller didCheck1:(BOOL *)MMbool didCheck2:(BOOL *)MMbool2;
@end
@interface MatchModeViewController : UIViewController {
BOOL checked1;
BOOL checked2;
}
@property BOOL checked1;
@property BOOL checked2;
@property (weak, nonatomic) IBOutlet UIButton *checkBoxButton1;
@property (weak, nonatomic) IBOutlet UIButton *checkBoxButton2;
- (IBAction)checkButton1:(id)sender;
- (IBAction)checkButton2:(id)sender;
@end
MatchModeViewController.m
@synthesize checkBoxButton1;
@synthesize checkBoxButton2;
@synthesize checked2;
@synthesize checked1;
- (void)viewDidLoad {
[super viewDidLoad];
checked1 = NO;
checked2 = NO;
[checkBoxButton1 setImage:[UIImage imageNamed:@"CheckBox.png"] forState: UIControlStateNormal];
[checkBoxButton2 setImage:[UIImage imageNamed:@"CheckBox.png"] forState: UIControlStateNormal];
UIBarButtonItem *saveButtonMM = [[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveButtonMMPressed)];
self.navigationItem.rightBarButtonItem = saveButtonMM;
}
-(instancetype) init {
if (self = [super init]) {
}
return self;
}
- (void) saveButtonMMPressed {
if([self.MMdelegate respondsToSelector:@selector(addItemViewControllerMM:didCheck1:didCheck2:)]){
[self.MMdelegate addItemViewControllerMM:self didCheck1: checked1 didCheck2: checked2 ];
[[NSNotificationCenter defaultCenter]postNotificationName:@"MMNOTIFICATIONNAME" object:nil];
}
[self.navigationController popViewControllerAnimated:YES ];
[self performSelector:@selector(saveButtonMMPressed) withObject:nil afterDelay:0.25];
}
我是xcode的初学者,非常感谢任何帮助。
答案 0 :(得分:2)
您使用指向BOOL
的指针错误地定义了协议方法。你想要:
@protocol MatchModeViewControllerDelegate <NSObject>
- (void)addItemViewControllerMM:(MatchModeViewController *)MMcontroller didCheck1:(BOOL)MMbool didCheck2:(BOOL)MMbool2;
@end
同时更新此方法的任何实现以删除指针。
答案 1 :(得分:1)
这更像是一个C问题。 看看你的方法:
- (void)addItemViewControllerMM:(MatchModeViewController *)MMcontroller didCheck1:(BOOL *)MMbool didCheck2:(BOOL *)MMbool2;
似乎MMbool
和MMbool2
应该是输出参数,您需要通过引用传递它们。
所以,不要这样打电话:[self.MMdelegate addItemViewControllerMM:self didCheck1:checked1 didCheck2: checked2];
您应该这样称呼它:[self.MMdelegate addItemViewControllerMM:self didCheck1:&checked1 didCheck2:&checked2 ];
在&
之前放置checked1
发送变量的引用而不是值,因此您在方法中对checked1
所做的任何更改都会在调用它的函数中显示
LE:
或者您可能不需要通过引用传递它们。在这种情况下,你应该将协议方法重新定义为- (void)addItemViewControllerMM:(MatchModeViewController *)MMcontroller didCheck1:(BOOL)MMbool didCheck2:(BOOL)MMbool2;
(没有*使得BOOLs指针)