How to use delegate pattern on iPhone

2019-03-02 04:43发布

I have some questions about using delegate patten on iPhone.

This is code using delegate patten. This code works.

SecondViewController *secondViewController = [[SecondViewController alloc] init];   
    secondViewController.delegate = self;
    [self.navigationController pushViewController:secondViewController animated:YES];
    [secondViewController release];

I wrote code like this. But it doesn't work. I thought that delegate is not valid.

GoogleDocsViewController *googleDocsView = [[GoogleDocsViewController alloc]initWithNibName:@"GoogleDocsViewController" bundle:nil];
    googleDocsViewController.delegate = self;
    [self.navigationController pushViewController:googleDocsView animated:YES];
    [googleDocsView release]; 

I didn't know that why this code have warning. Warning like this.

Class 'RootViewController' does not implement the 'GoogleDocsViewControllerDelegate' protocol

What can I do ?

ADD SOURCE CODE

GoogleViewController.h

@protocol GoogleDocsViewControllerDelegate;
@interface GoogleDocsViewController : UIViewController<UITextFieldDelegate> {       
    id<GoogleDocsViewControllerDelegate> delegate;
}
@property (nonatomic,assign) id<GoogleDocsViewControllerDelegate> delegate;

@end

@protocol GoogleDocsViewControllerDelegate<NSObject>;

@required

-(void) googleViewControllerInputDidEnd:(NSString *)text;

@end

googleDocsViewController.m

-(void)googleViewControllerInputDidEnd:(NSString *)text{
    NSLog(@"delegate"); 
}

This googleViewControllerInputDidEnd:(NSString*)text method is test source.

1条回答
Fickle 薄情
2楼-- · 2019-03-02 05:07

Can you tell us which class is creating the GoogleDocsViewController? Check that class' header (.h file) and see if it implements the GoogleDocsViewControllerDelegate.

Basically the delegate is the object which will listen to interesting events from the GoogleDocsViewController and it should implement the protocol mentioned so it "knows" which actions to perform when the events are fired.

查看更多
登录 后发表回答