Recently read this:
Passing Data between View Controllers
Which outlines delegate setup between two controllers. The problem I am running into is that I do not have a segue between the controllers. And I am not sure I wan ta segue between those two controllers, ie I do not want to change the view when a value updates, behind the scenes I just want another controller to be aware that the value did indeed change.
This is the step I am stumbling on from the link above:
The last thing we need to do is tell ViewControllerB that ViewControllerA is its delegate before we push ViewControllerB on to nav stack.
ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil]; viewControllerB.delegate = self [[self navigationController] pushViewController:viewControllerB animated:YES];
I am not using nibs, nor do I think a prepare for segue is the correct place to wire the delegate up. Is there some other place I am supposed to wire up the delegate?
here is my code: SettingsVeiwController, want to let another controller know when the user updates the refresh rate field.
#import <UIKit/UIKit.h>
@protocol SettingsViewControllerDelegate <NSObject>
-(void) didUpdateRefreshRate:(NSString *)refreshRate;
@end
@interface SettingsViewController : UITableViewController <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *refreshRateTextField;
@property (copy, nonatomic) NSNumber *refreshRate;
@property (weak, nonatomic) id <SettingsViewControllerDelegate> delegate;
@end
MainViewController, want to get updates when refreshRate changes,
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) didUpdateRefreshRate:(NSString *)refreshRate {
NSLog(@"This was returned from SettingsViewController %@",refreshRate);
}
@end
I think everything is setup correctly to work except telling SettingsViewController that the MainViewController is its delegate.
Probably most important is that this is a tabbed application. MainViewController is one tab, SettingsViewController is another. Might be a good way to set this up when using a tabbed application, ie how do I pass info/data between tabs. I assume it via delegates still just need to know where to wire them together.