Reputation:
I'm just starting to learn Objective-C and I'm not 100% on all the syntax yet, and I think that might be where I'm having trouble, then again I'm not sure. I'm trying to get one view controller to send a message to its parent/callee view controller. I have it set up like this:
//ParentViewController.h
#import "SubViewController.h"
@interface ParentViewController : UIViewController <SubViewControllerDelegate>
- (void) sendMessageToParent:(NSInteger)num;
@end
This is the implementation:
//ParentViewController.m
@implementation ParentViewController
- (void) sendMessageToParent:(NSInteger)num
{
NSLog(@"Message is %d", num);
}
- (void) buttonPushed
{
[self performSegueWithIdentifier:@"SubView" sender:sender];
}
@end
Here's the other view controller setup:
//SubViewController.h
@protocol SubViewControllerDelegate <NSObject>
- (void) sendMessageToParent:(NSInteger)num;
@end
@interface SubViewController : UIViewController
@property (nonatomic, weak) id <SubViewController> delegate;
@end
And implementation:
//SubViewController.m
@implementation SubViewController
@synthesize delegate;
- (void) something
{
[self.delegate sendMessageToParent:4];
[self.dismissViewControllerAnimated:YES completion:nil];
}
Appreciate any help, thanks.
Upvotes: 0
Views: 44
Reputation: 17094
Looks like you're not setting SubViewController's delegate to SuperViewController. You'd do this typically when you instantiate SubViewController. e.g. subViewController.delegate = self
ParentViewController.h
@interface ParentViewController : UIViewController
@end
ParentViewController.m
@implementation ParentViewController <SubViewControllerDelegate> // Better to place this in .m than .h
#pragma mark - SubViewController Delegate
- (void)sendMessageToParent:(NSInteger)num
{
NSLog(@"Message is %d", num);
}
- (void)buttonPushed
{
[self performSegueWithIdentifier:@"SubView" sender:sender];
}
@end
SubViewController.h
@protocol SubViewControllerDelegate <NSObject>
- (void)sendMessageToParent:(NSInteger)num;
@end
@interface SubViewController : UIViewController
@property (nonatomic, weak) id<SubViewController> delegate;
@end
SubViewController.m
@implementation SubViewController
// @synthesize delegate; // Not necessary, old fashioned
- (void)something
{
// Set a breakpoint here and step through, you're not setting your delegate, so this conditional is not being hit
if (self.delegate && [self.delegate respondsToSelector:@selector(sendMessageToParent:)])
{
[self.delegate sendMessageToParent:4];
}
[self.dismissViewControllerAnimated:YES completion:nil];
}
Upvotes: 0
Reputation: 1988
You need to add to ParentViewController:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"SubView"])
{
SubViewController *vc = [segue destinationViewController];
vc.delegate = self;
}
}
Upvotes: 4
Reputation: 17850
You need to add handler for prepareForSegue
and set .delegate
properly there. I don't think your SubviewController has delegate set to the parent one.
Upvotes: 0