Xcode 4.2 - 丢失数组对象

时间:2012-01-24 23:34:54

标签: xcode ios4 ios5 xcode4.2

我是Xcode 4.2的新手,并且有一个Storyboard数组问题。我正在做一个名为DrinkMixer的教程,但是使用Storyboard而不是传统的XIB方法编写它。

此应用程序使用导航控制器,推送到详细视图(DetailViewController)的表视图(MasterViewController),以及以模态方式呈现的添加饮料视图(AddDrinkViewController)。

桌子视图(从桌子上挑选饮料)和详细视图(显示所选饮料的详细信息)效果很好。但是当我尝试添加新饮料并按下“保存”按钮时,模态视图会正常消失,但表格视图不会更新新饮料。

我在MasterViewController中创建了对drinkArray的引用,并认为当我使用addObject放置新饮料时会保留我的数组,但我在某处错过了这条船。 MasterViewController中的断点显示我最初在drinkArray中有40个对象但是当我到达AddDrinkViewController时,drinkArray有0个对象。

任何人都知道为什么我的drinkArray丢失了它的物品?提前感谢任何建议!

这是我的代码......

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];
}

1 个答案:

答案 0 :(得分:1)

我想问题出在这里:

 AddDrinkViewController *addViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"AddDrink"];

您不必创建视图控制器的新实例。这将自动处理。所以我猜你看到的View控制器不是你要分配数组的控制器。试试这个:

AddDrinkViewController* addViewController = (AddDrinkViewController*) segue.destinationViewController;
addViewController.drinkArray = self.drinks;