I have:
UITableViewControllerA
(Lists different ways to refine database product results)
UIViewControllerB
(Controller associated with one of the rows from list above)
BaseController (Subclass of UIViewController
, contains common code for controllers associated with rows in UITableViewControllerA
and is their super class)
I can pass data to UIViewControllerB
from UITableViewControllerA
easily using the perpareForSegue method. I am now trying to pass data from UIViewControllerB
to UITableViewControllerA
when popViewControllerAnimated is called in my UIButton
custom method that a user taps once they have made a selection of what to refine database product results with.
I decided to use delegation. UIViewControllerB
which is subclass of a BaseController and in UITableViewControllerA
I mentioned above that I have a list of ways to refine products:
Gender - Size - Colour - Price - Type
Rather than define my protocol below in each of their controllers I decided to define it in the BaseController like so:
#import <UIKit/UIKit.h>
@protocol BaseViewControllerDelegate <NSObject>
- (void)didTapDoneButtonWithSelection:(NSString *)selection; // use string for now to test
@end
@interface BaseViewController : UIViewController
@property (nonatomic, retain) id <BaseViewControllerDelegate> delegate;
- (UIButton *)clearButton;
- (UIButton *)doneButton;
@end
Then I went over to UIViewControllerB and accessed the delegate property and passed in the string to be sent over to UITableViewControllerA when done button is tapped:
- (void)doneButtonTapped:(id)sender {
NSLog(@"DONE BUTTON TAPPED");
[[self delegate] didTapDoneButtonWithSelection:@"THIS IS A TEST"];
[[self navigationController] popViewControllerAnimated:YES];
}
In UITableViewControllerA I conform to the delegate protocol:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "BaseController.h"
@interface UITableViewControllerA : UITableViewController <BaseViewControllerDelegate>
@end
In UITableViewControllerA's implementation file I declare an instance var to hold the data passed over from the UIViewControllerB and display in a log message in viewWIllAppear. The didTapDoneButtonWithSelection method gets the data from the argument and stores it in the _test var:
@interface UITableViewControllerA ()
@end
@implementation UITableViewControllerA
{
NSString *_test;
}
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"%@", _test);
}
- (void)didTapDoneButtonWithSelection:(NSString *)selection
{
_test = selection;
}
Issue:
My log message is giving me (null).
Is what I'm doing possible? or am I going about it the wrong way. If so please correct me with a code example.
Thanks for your time
Regards