Xcode 4.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];
}
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想问题就在这里:
您不必创建视图控制器的新实例。这将被自动处理。所以我猜你看到的视图控制器不是你分配数组的控制器。试试这个:
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: