I am learning how to use KVO. I created two classes, Truck and Driver as shown below.
the Truck class has a textfield and a button, the text should contain the current truck speed, and when the button is pressed, prepareForSegue
should be called and it contains the code posted below.
the Driver class contain a textfield that should be filled with the truck speed. the truck speed in Truck class will be passed to the textfield in Driver class through KVO as shown in the code.
the problem I have or what I am trying to do is, when the user enters a speed of the truck in the Truck class and presses the button, I want to display the truck speed entered in the textfield in the Driver class through KVO
the result I am getting according to the code posted below is, an empty textfield in the Driver class
please let me know why the textfield in the driver class is empty. and how should I pass the value of the truck speed from Truck class to Driver class through KVO
Truck.m
#import "TruckViewController.h"
#import "DriverViewController.h"
#import "ServerViewController.h"
@interface TruckViewController ()
@property (strong, nonatomic) IBOutlet UITextField *textFieldCurrenSpeed;
@property (strong, nonatomic) IBOutlet UIButton *buttonBroadcast;
@property (strong, nonatomic) IBOutlet UIButton
*buttonToDriverViewController;
@property (strong, nonatomic) IBOutlet UIButton
*buttonToServerViewController;
@property (strong, nonatomic) TruckViewController *truck;
@property (strong, nonatomic) DriverViewController *driver;
@property (strong, nonatomic) ServerViewController *server;
@end
@implementation TruckViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
self.truck = [[TruckViewController alloc] init];
self.driver = [[DriverViewController alloc] init];
self.server = [[ServerViewController alloc] init];
if ([segue.identifier isEqualToString:@"segueToDriver"]) {
[self.truck addObserver:self.driver
forKeyPath:@"currentSpeedOfTheTruck"
options:NSKeyValueObservingOptionNew
context:NULL];
self.truck.currentSpeedOfTheTruck = [self.textFieldCurrenSpeed
text];
NSLog(@"prepareForSegue: %@", self.truck.currentSpeedOfTheTruck);
}
if ([segue.identifier isEqualToString:@"segueToServer"]) {
[self.truck addObserver:self.server
forKeyPath:@"currentSpeedOfTheTruck"
options:NSKeyValueObservingOptionNew
context:NULL];
self.truck.currentSpeedOfTheTruck = [self.textFieldCurrenSpeed
text];
NSLog(@"text entered: %@", self.truck.currentSpeedOfTheTruck);
}
}
- (void)dealloc
{
[self.truck removeObserver:self.driver
forKeyPath:@"currentSpeedOfTheTruck"];
[self.truck removeObserver:self.server
forKeyPath:@"currentSpeedOfTheTruck"];
}
Driver.m
#import "DriverViewController.h"
@interface DriverViewController ()
@property (strong, nonatomic) IBOutlet UITextField
*textFieldCurrentSpeed;
@end
-(void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id>
*)change
context:(void *)context {
if ([keyPath isEqualToString:@"currentSpeedOfTheTruck"]) {
NSLog(@"DriverViewController->currentSpeedOfTheTruck: %@",
[object valueForKey:keyPath]);
NSLog(@"DriverViewController->currentSpeedOfTheTruck: %@",
[change objectForKey:NSKeyValueChangeNewKey]);
self.receivedCurrentSpeed = [change
objectForKey:NSKeyValueChangeNewKey];
[self.textFieldCurrentSpeed setText:self.receivedCurrentSpeed];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"DriverViewController->viewDidLoad");
[self.textFieldCurrentSpeed setText:self.receivedCurrentSpeed];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
log output:
2017-08-13 19:53:55.293 KVC-1[91529:2346883] DriverViewController-
>currentSpeedOfTheTruck: 45
2017-08-13 19:53:55.294 KVC-1[91529:2346883] DriverViewController-
>currentSpeedOfTheTruck: 45
2017-08-13 19:53:55.294 KVC-1[91529:2346883] prepareForSegue: 45