I’m new to Xcode 4.2 and have a Storyboard array question. I’m doing a tutorial called DrinkMixer but coding it with Storyboard instead of the traditional XIB approach.
This app uses the navigation controller, a table view (MasterViewController) that pushes to a detail view (DetailViewController), and an add-drink view (AddDrinkViewController) presented modally.
The table view (pick a drink from a table) and detail view (shows details on chosen drink) works great. But when I try to add a new drink and press the “Save” button, the modal view goes away properly but the table view is not updated with the new drink.
I’ve created a reference to my drinkArray in the MasterViewController and thought that would maintain my array when I place the new drink using addObject, but I’m missing the boat somewhere. Breakpoints within MasterViewController shows that I initially have 40 objects within my drinkArray but when I get to the AddDrinkViewController the drinkArray has 0 objects.
Anyone know why my drinkArray loses its objects? Thanks in advance for any advice!
Here’s my code…
MasterViewController.h
#import <UIKit/UIKit.h>
@interface MasterViewController : UITableViewController {
NSMutableArray *drinks;
}
@property (nonatomic, retain) NSMutableArray *drinks;
@end
MasterViewController.m
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "AddDrinkViewController.h"
#import "DrinkConstants.h"
@implementation MasterViewController
@synthesize drinks;
@synthesize masterUIView;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"DrinkDirections" ofType:@"plist"];
drinks = [[NSMutableArray alloc] initWithContentsOfFile:path];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
.
.
.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"SegueAddDrink"]) {
AddDrinkViewController *addViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"AddDrink"];
addViewController.drinkArray = self.drinks;
}
}
AddDrinkViewController.h
#import <UIKit/UIKit.h>
@interface AddDrinkViewController : UIViewController {
BOOL keyboardVisible;
NSMutableArray *drinkArray_;
}
- (void)keyboardDidShow:(NSNotification *)notif;
- (void)keyboardDidHide:(NSNotification *)notif;
@property (nonatomic, retain) NSMutableArray *drinkArray;
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UITextField *nameTextField;
@property (nonatomic, retain) IBOutlet UITextView *ingredientsTextView;
@property (nonatomic, retain) IBOutlet UITextView *directionsTextView;
- (IBAction) save: (id) sender;
- (IBAction) cancel: (id) sender;
@end
AddDrinkViewController.m
#import "MasterViewController.h"
#import "AddDrinkViewController.h"
#import "DrinkConstants.h"
@implementation AddDrinkViewController
@synthesize drinkArray = drinkArray_;
@synthesize scrollView;
@synthesize nameTextField;
@synthesize ingredientsTextView;
@synthesize directionsTextView;
.
.
.
- (IBAction) save: (id) sender {
// Create a new drink dictionary for the new values.
NSMutableDictionary *newDrink = [[NSMutableDictionary alloc] init];
[newDrink setValue:self.nameTextField.text forKey:NAME_KEY];
[newDrink setValue:self.ingredientsTextView.text forKey:INGREDIENTS_KEY];
[newDrink setValue:self.directionsTextView.text forKey:DIRECTIONS_KEY];
[drinkArray_ addObject:newDrink];
// Remove the modal view and go back to the table view.
[self dismissModalViewControllerAnimated:YES];
}
I guess the problem is here:
You dont have to create a new instance of the view controller. This will be handled automatically. So I guess the View controller you see is not the one you are assigning the array to. Try this instead: