I am having some issues getting the hang of using delegates with Storyboards in an app I'm working on. I've reviewed some other searches, including Passing data back :Delegate method not called, but to no avail. At this point, I've deleted everything in my app, except for the the basics of my two views. The first view is a basic view controller with a button that when pushed, segues to a modal view, where one pice of data is entered in a textfield. A Save button is on that modal view, that when pushed, returns back to the first view. Of course, I want to send that entered data back to the first view as well. Sounds simple? Sure, but somewhere I can't get it to work. It seems my delegate call is never made. Here's the code:
MyDayViewController.h - this is the first view controller
#import <UIKit/UIKit.h>
#import "AddRatingModalViewController.h"
@interface MyDayViewController : UIViewController <AddRatingDelegate>
@property (strong, nonatomic) IBOutlet UIButton *addRatingBtn;
@property (strong, nonatomic) IBOutlet UIButton *addAccompBtn;
@property (strong, nonatomic) IBOutlet UILabel *ratingLabel;
@end
MyDayViewController.m
#import "MyDayViewController.h"
@implementation MyDayViewController
@synthesize addRatingBtn;
@synthesize addAccompBtn;
@synthesize ratingLabel;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ToRatingVC"])
{
NSLog(@"Setting MyDayVC as a delegate of AddRatingMVC");
AddRatingModalViewController *addRatingController = segue.destinationViewController;
addRatingController.delegate = self;
}
}
- (void)theSaveButtonTapped:(AddRatingModalViewController *)controller withRating:(NSNumber*) rating
{
NSLog(@"In theSaveButtonTapped method of MyDayVC");
ratingLabel.text = [[NSString alloc]initWithFormat:@"%@", rating];
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
[self setAddRatingBtn:nil];
[self setAddAccompBtn:nil];
[self setRatingLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
AddRatingModalViewController.h - this is the modal view controller
#import <UIKit/UIKit.h>
@class AddRatingModalViewController;
@protocol AddRatingDelegate
- (void)theSaveButtonTapped:(AddRatingModalViewController *)controller withRating:(NSNumber*) rating;
@end
@interface AddRatingModalViewController : UIViewController
@property (nonatomic, weak) id <AddRatingDelegate> delegate;
@property (strong, nonatomic) IBOutlet UITextField *addRatingTextField;
- (IBAction)cancelBtnPressed:(id)sender;
- (IBAction)saveBtnPressed:(id)sender;
@end
AddRatingModalViewController.m
#import "AddRatingModalViewController.h"
@implementation AddRatingModalViewController
@synthesize addRatingTextField;
@synthesize delegate;
- (IBAction)cancelBtnPressed:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)saveBtnPressed:(id)sender {
NSNumber *ratingARMVC = [NSNumber numberWithInteger:[addRatingTextField.text integerValue]];
NSLog(@"The entered Rating in ARMVC is %@", ratingARMVC);
NSLog(@"Telling the MyDayVC Delegate that Save was tapped on the AddRatingTVC");
[self.delegate theSaveButtonTapped:self withRating:ratingARMVC];
}
- (void)viewDidUnload
{
[self setAddRatingTextField:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
Output from the console is as such:
2012-07-16 16:49:16.384 MyDayDelTest[4695:f803] Setting MyDayVC as a delegate of AddRatingMVC
2012-07-16 16:49:39.274 MyDayDelTest[4695:f803] The entered Rating in ARMVC is 7
2012-07-16 16:49:39.275 MyDayDelTest[4695:f803] Telling the MyDayVC Delegate that Save was tapped on the AddRatingTVC
This is right before the delegate call is supposed to be made in the Save method. Any help would be greatly appreciated! I should say that tests work when I don't use storyboards, but I do need this to work with Storyboards, as the rest of my app uses them. Thanks in advance.