For a project I need to be able to send a status back from "secondTableViewController" to "firstTableViewController". I followed some tutorials on delegates but I don't seem to get it to work. When I want to call the method in firstTableViewController from the secondTableViewController I do a check to see if "delegate" property is empty, and every time it is..
This is my code:
SecondTableViewController.h
@protocol SendStatusDelegate <NSObject>
@required
-(void)didStatusChanged;
@end // eof delegate protocol
@interface SecondTableViewController : UITableViewController
@property (nonatomic, assign) id<SendStatusDelegate> delegate;
@end
SecondTableViewController.m
// Status is changed on row click
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.delegate) { // This is never called because delegate looks to remain empty
[self.delegate didStatusChanged];
}
}
FirstTableViewController.h
@interface FirstTableViewController : UITableViewController <SendStatusDelegate>
@end
FirstTableViewController.m
The only thing that goes on here is implementing the -(void)didStatusChanged method.
First You need to set the delegate
to self in FirstTableViewController
when you init the SecondTableViewController
. say on didSelectRowAtIndexPath:
you are calling the SecondTableViewController
in that case,
FirstTableViewController.m
// Status is changed on row click
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondTableViewController *secondTableVC = [SecondTableViewController alloc] init];
secondTableVC.delegate = self;
}
In the FirstTableViewController
where you create the SecondTableViewController
instance you should assign the delegate of the created instance to FirstTableViewController
ie
secondTableViewControllerInstance.delegate = self;
in FirstTableViewController
Your FirstTableViewController
Class should conform the SendStatusDelegate
Like
@interface FirstTableViewController : UITableViewController <SendStatusDelegate>
And then then assign the delegate
of SecondViewController
instance by self
Like
SecondTableViewControllerInstance.delegate = self;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.