我正在使用示例学习协议和代表!当我尝试重新创建此示例时,我注意到不遵守此条件:
if([delegate respondsToSelector:@selector(amountEntered:)]) {blabla}
错误在哪里?脚本:
第一次查看.h
#import <UIKit/UIKit.h>
#import "EnterAmountViewController.h"
@interface DelegateExampleViewController : UIViewController <EnterAmountDelegate>{
IBOutlet UILabel *amountLabel;
}
-(IBAction)changeAmountPressed;
@end
第一次查看.m
#import "DelegateExampleViewController.h"
@implementation DelegateExampleViewController
-(IBAction)changeAmountPressed
{
EnterAmountViewController * enterAmountVC = [[EnterAmountViewController alloc]init];
enterAmountVC.delegate = self;
}
-(void)amountEntered:(NSInteger)amount
{
amountLabel.text = [NSString stringWithFormat:@"%i" , amount];
}
@end
第二视图.h
#import <UIKit/UIKit.h>
@protocol EnterAmountDelegate <NSObject>
-(void)amountEntered:(NSInteger)amount;
@end
@interface EnterAmountViewController : UIViewController {
IBOutlet UITextField *amountTextField;
id<EnterAmountDelegate> delegate;
}
-(IBAction)savePressed;
@property(nonatomic,retain) id<EnterAmountDelegate> delegate;
@end
第二视图.m
#import "EnterAmountViewController.h"
#import "DelegateExampleViewController.h"
@implementation EnterAmountViewController
@synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
amountTextField.text = @"";
[amountTextField becomeFirstResponder];
}
-(IBAction)savePressed
{
if([delegate respondsToSelector:@selector(amountEntered:)])
{
[delegate amountEntered:[amountTextField.text intValue]];
NSLog(@"rugg");
}
}
@end
提前致谢!