2014-05-19 35 views
0

我想获得一个协议工作,但有一些问题。无法正确地获得协议设置

我的协议class.h

@protocol FormViewDelegate <NSObject> 
// sent when the user selects a row in the recent searches list 
@required 
- (void)getDirections:(NSString*)address :(NSString*)cityStateZip; 


@end 


@interface BaseFormViewController : NSObject 
@property (nonatomic, weak) id<FormViewDelegate> delegate; 


@end 

我viewcontroller.h

#import "BaseFormViewController.h" 

@interface ViewController1 : <FormViewDelegate> 

我viewcontroller.m

@implementation ViewController1 
{ 
    BaseFormViewController *baseProtocol; 
} 

- (IBAction)getDirections:(id)sender { 

    [baseProtocol getDirections:self.address.text :self.cityStateZip.text]; 

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    baseProtocol = [[BaseFormViewController alloc]init]; 
    baseProtocol.delegate = self; 
    ... 
} 

我得到一个编译器错误指出:'BaseFormViewController'没有可见@interface声明选择器'getDirections ::'我在这里做错了什么?

回答

0

如果我是你,我会那样做:

@protocol FormViewDelegate <NSObject> 
// sent when the user selects a row in the recent searches list 
@required 
- (void)getDirections:(NSString*)address :(NSString*)cityStateZip; 
@end 

那么你的观点或者Controller.h

@interface ViewController1 : <FormViewDelegate> 
@property (nonatomic, weak) id<FormViewDelegate> delegate; 
@end 

,然后你的视图控制器

@implementation ViewController1 

@synthesize delegate 

- (IBAction)getDirections:(id)sender { 

    [baseProtocol getDirections:self.address.text :self.cityStateZip.text]; 

} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    baseProtocol = [[BaseFormViewController alloc]init]; 
    baseProtocol.delegate = self; 
    ... 
} 

这样你的视图控制器实现该协议本身

希望它能帮助

0

ViewController1需要实现所需的方法,因为您声明它符合FormViewDelegate协议。

@implementation ViewController1 

- (void)getDirections:(NSString*)address :(NSString*)cityStateZip 
{ 
// your implementation 
} 

@end 
0

在这里,因为你已经在@Required标签上写入,那么你必须编写该方法认定中,否则会给你警告,这是你得到。

您可以在两个标签下编写方法。 1)@required:在这个标签中,你声明的所有方法都必须在你的类中指定。

2)@optional:在这个标签中,你所声明的所有方法都是可选的方法,如果你不写也不会得到警告。

+0

我在需要或没有我仍然得到一个生成错误 – BluGeni

+0

你得到什么错误? –

0

就这么简单。 BaseFormViewController不符合FormViewDelegate - 因此它没有关于这种方法的知识:如果你想给这个方法传递给BaseFormViewController比使其符合FormViewDelegate

- (void)getDirections:(NSString*)address :(NSString*)cityStateZip;

。并(可选)将BaseFormViewController的委托人财产移至ViewController1