Xcode 4.2 - 丢失数组对象

发布于 2024-12-28 13:42:24 字数 3396 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2025-01-04 13:42:24

我想问题就在这里:

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

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

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

I guess the problem is here:

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

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:

AddDrinkViewController* addViewController = (AddDrinkViewController*) segue.destinationViewController;
addViewController.drinkArray = self.drinks;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文