Basically, I am now intend to use delegate to pass values between view controllers.
The flow of the view controllers is A -> B -> C
When the user does some action in the "C" view controller, how to pass the value back to the first view controller, which is "A"?
In my own code, the delegate method is never triggerred and "self.delegate" is always "null". I am not sure why and how to solve this problem.
Below is the code of the First VC and Third VC:
#import "ViewController.h"
#import "ThirdViewController.h"
@interface ViewController ()<PassValueProtocal>
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
@implementation ViewController
{
ThirdViewController *thirdVC;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) passValueBack:(NSString *)value
{
NSLog(@"HAHAH");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)segueToSecondVC:(UIButton *)sender
{
thirdVC = [ThirdViewController sharedManager];
thirdVC.delegate = self;
}
@end
#import "ThirdViewController.h"
@interface ThirdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *myTextField;
@end
@implementation ThirdViewController
+ (id) sharedManager
{
NSLog(@"myDelegate sharedManager");
static ThirdViewController *sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ sharedManager = [[self alloc] init]; });
return sharedManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)passValueAction:(UIButton *)sender
{
NSLog(@"%@", self.delegate);
if ([self.delegate respondsToSelector:@selector(passValueBack:)])
{
[self.delegate passValueBack:self.myTextField.text];
}
}