So you want two Objects to talk to each other more than a little bit? Here is what to do by example. Lets say we have our MainViewController and a new SearchViewController that you want a delegate attached to it. This is all you need to do.
In SearchViewController.h
Helpful link
http://stackoverflow.com/questions/12020539/how-to-make-custom-delegate-in-ios-app
In SearchViewController.h
@protocol SearchViewDelegate <NSObject>
@optional
@required
-(void)didAskToSearch:(NSString*)string;
@end
@interface SearchViewController : UIViewController {
}
@property (nonatomic, assign) id <SearchViewDelegate> delegate;
@end
optional has non-required methods and required of course has required methods.
In SearchViewController.m;
[_delegate didAskToSearch: [self searchRequestDetails]];
this is put anywhere in the code when you want it to send the information back to the other object.
In the MainViewController.h;
@interface MainViewController : UIViewController <SearchViewDelegate> {
}
In MainViewController.m;
SearchViewController *searcher = [[SearchViewController alloc]init];
searcher.delegate = self;
#pragma mark - SearchViewControllerDelegates
-(void)didAskToSearch:(NSString*)string{
//do whatever you want with the string results
}
When creating the object, make sure the tell the delegate you have those methods. Then you just have to create the methods as per it is required.
Helpful link
http://stackoverflow.com/questions/12020539/how-to-make-custom-delegate-in-ios-app
No comments:
Post a Comment