Michael
Michael

Reputation: 277

Objective-C Delegate Memory Storage null Issue

I've been having trouble with a delegate between two UIViewControllers in Objective-C.

I've created the delegate in one of the UIViewControllers that talks back to the previous UIViewController. I declared the delegate as such in the ConfirmDetailsViewController. (This was declared in the viewDidLoad, along with the rest of the delegate code):

ForthViewController *delegate = [[ForthViewController alloc] init];

I've also included the correct #import "ForthViewController.h" above the @interface of the .m file.

A few lines later in the I'm setting a group of local property NSStrings to the value of delegate.nameString, which is another group of property NSStrings from the ForthViewController, as such:

self.nameString = delegate.nameString;
self.emailAddressString = delegate.emailAddressString;
self.phoneNumberString = delegate.phoneNumberString;
self.emergencyPhoneNumberString = delegate.emergencyPhoneNumberString;
self.additionalDonationString = delegate.additionalDonationString;

I then go on to print the values of self.nameString, self.emailAddressString, etc to the console with NSLog as such:

NSLog(@"Name: %@", self.nameString);
NSLog(@"Email Address: %@", self.emailAddressString);
NSLog(@"Phone Number: %@", self.phoneNumberString);
NSLog(@"Emergency Phone Number: %@", self.emergencyPhoneNumberString);
NSLog(@"Donation: $%@", self.additionalDonationString);

In the ForthViewController after assigning the values of the local property objects to self.nameString, etc. I print their values to the console and get the appropriate values. When I then transfer the objects to the ConfirmDetailsViewController, I'm confronted with (null) values for all of the strings.

I believe the most likely fault is in how the delegate is declared in the ConfirmDetailsViewController, however I've seen multiple sources online that say this is how you setup the delegate. How would I get the proper values to siphon through the delegate properly without loosing their values and resetting to null?

In case you had trouble with my explanation, here is a download link to a sample project where this issue is apparent: https://www.dropbox.com/sh/66x89dciu4waxyd/AADdA3ehUglWIDWZGzG08zqpa

EDIT

For those who were wondering, this is how I declared the NSString properties in both the .h file of ForthViewController and .h file of ConfirmDetailsViewController:

@property (strong, nonatomic) NSString *nameString;
@property (strong, nonatomic) NSString *emailAddressString;
@property (strong, nonatomic) NSString *additionalDonationString;
@property (strong, nonatomic) NSString *phoneNumberString;
@property (strong, nonatomic) NSString *emergencyPhoneNumberString;

Upvotes: 0

Views: 90

Answers (1)

architectpianist
architectpianist

Reputation: 2552

As far as I can tell, you're not getting good values out of the ForthViewController because you created it using alloc/init. The object you have as "delegate" is not the same object that has proper values for the properties you want. In the sample project you provided, for instance, the saveButton: method would never be called for the object you have set as delegate.

So, how should you set it up? Use the @protocol syntax to create a real delegate. Define the protocol in the second view controller's header, and make the first view controller subscribe to that protocol. Then add a delegate property of the second view controller that requires its value to conform to the protocol. When you create an instance of the second view controller, set its delegate property to self to make the connection. Now, in your second view controller code, you can safely call any of the methods you have listed in the protocol on the delegate object.

This is a delegation pattern because the second view controller can dispatch messages to an object that it doesn't know anything about. All it knows is that its delegate can receive its messages.

Here is the scoop on protocols, straight from the horse's mouth.

Upvotes: 1

Related Questions