来自IBAction的AppDelegate addObject中的NSMutableArray

时间:2011-01-03 12:14:42

标签: iphone arrays nsmutablearray

我一直在尝试为我的应用程序添加一个简单的书签工具,用于我在uni的学位论文。我是iPhone开发的新手,所以我真的不明白发生了什么。

我想要做的是将一个对象(字符串)添加到数组中,但我所做的事情根本不起作用。

我在AppDelegate中设置了数组,并在BookmarksViewController.m文件中填充了一个表视图。

我的应用程序包含一个标签栏和导航栏,但我已将其全部删除,以便于阅读。

以下是代码:

设置阵列

DissertationAppDelegate.h

#import <UIKit/UIKit.h>

@class DissertationViewController;

@interface DissertationAppDelegate : NSObject <UIApplicationDelegate> {

    NSMutableArray *array;

    UIWindow *window;
    DissertationViewController *viewController;
}

@property (nonatomic, retain) UIWindow *window;

@property (nonatomic, retain) NSMutableArray *array;

@end

DissertationAppDelegate.m

#import "DissertationAppDelegate.h"

@implementation DissertationAppDelegate

@synthesize window;

@synthesize array;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions {    

    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fullFileName = [NSString stringWithFormat:@"%@/arraySaveFile", documentsDirectory];

    array = [[NSMutableArray alloc] init];
    [array addObject:@"One"];
    [array addObject:@"Two"];
    [array addObject:@"Three"];


    [array writeToFile:fullFileName atomically:NO];

    [self.window makeKeyAndVisible];
}

使用阵列展示桌面视图

BookmarksViewController.h

@interface BookmarksViewController : UITableViewController {
    NSMutableArray *bookmarksArray;
}

@property (nonatomic, retain) NSMutableArray *bookmarksArray;

BookmarksViewController.m

#import "BookmarksViewController.h"
@synthesize bookmarksArray;
- (void)viewDidLoad {

    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fullFileName = [NSString stringWithFormat:@"%@/arraySaveFile", documentsDirectory];
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];

    bookmarksArray = array;


[self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [bookmarksArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...


      cell.textLabel.text = [bookmarksArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)dealloc {
    [bookmarksArray release];
    [super dealloc];
}

从另一个视图控制器执行IBACTION

标题文件

-(IBAction) pushChap01Bookmark:(id)sender;

实施档案

-(IBAction) pushChap01Bookmark:(id)sender{

    DissertationAppDelegate *appDelegate = (DissertationAppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.array = [[NSMutableArray alloc] init];
    [appDelegate.array addObject:@"THIS IS WHAT I WANT TO ADD"];

    [self.navigationController pushViewController:bookmarksViewController animated:YES];
    bookmarksViewController.title = @"Bookmarks";

}

Okkkkkkkkk ....所以当你按下按钮我希望它添加到app delegate中的数组并将BookmarksViewController推送到导航堆栈,并更新表。除了将对象添加到数组之外,它会执行所有操作。

如果有人知道什么是错的,那么请你帮帮我。我会非常感激。

谢谢

2 个答案:

答案 0 :(得分:0)

你确实意识到每次调用IBAction时你都在创建一个新的数组对象,保留它两次而不释放它?

这会造成内存泄漏,但更重要的是,每次你只想添加一些内容时,它都会用新的数组替换整个数组(从不发布旧数组)。所以你实际上并没有添加任何东西,而是你正在创建一个包含一个对象的数组。

操作方法是初始化像数组之类的东西的坏地方。将init代码移动到applicationDidFinishLaunching方法或其他一些理智的地方,并且在使用(保留)访问器将其分配给您的属性后,不要忘记将其释放。

编辑:您实际上是在applicationDidFinishLaunching中执行init内容,但是您没有将结果数组分配给appdelegates属性,以致它将丢失。将其分配给您的财产并删除您的IBAction中的任务,这应该有所帮助。

编辑2:我的坏了,你实际上是将数组分配给你的ivar,而不是使用访问器。 如果正确显示数组的前几个元素,并且在按下按钮后它们仍保留在tableview中,那么您的IBAction可能没有被调用或者您的tableview没有重新加载。您的代码应该实际上删除所有条目,只用一个新条目替换它们。将NSLog放入您的IBAction以检查是否正在调用它,如果没有,请检查您的连接。如果是,请检查tableview是否实际正在重新加载。

Edit3(响应您的评论):在添加新字符串后,尝试将以下行添加到您的IBAction中..:

NSLog(@"The array contains %i objects.", [[appDelegate array] count]);

这应该向控制台输出您的数组包含多少个对象 - 如果有,并且每次按下按钮时数字都会增加,那么表重新加载数据就会出错。 我怀疑你将数据“传输”到bookmarksviewcontroller的方式可能是导致问题的原因 - 你只是将appdelegates数组内容从viewdidload中的磁盘加载到bookmarksarray中。如果未再次调用viewdidload,则bookmarksarray中的数据也不会刷新。尝试重新读取viewWillAppear中的文件(或者在tableview重新加载之前调用的其他一些方法)。我强烈建议您找到另一种存储数据的方法 - 您的方法容易出现各种错误。尝试进入NSUserdefaults或核心数据。 但是,出于测试目的,请尝试替换此行

cell.textLabel.text = [bookmarksArray objectAtIndex:indexPath.row];
你的cellForRow代码的

代码包含以下代码:

DissertationAppDelegate *appDelegate = (DissertationAppDelegate *)[[UIApplication sharedApplication] delegate];
[[cell textLabel] setText:[[appDelegate array] objectAtIndex:indexPath.row]];

让我知道,会发生什么。

答案 1 :(得分:0)

pushChap01Bookmark 中执行此操作:

appDelegate.array = [[NSMutableArray alloc] init];

在appDelegate中重新创建数组(因此它又有0个项目)。 相反,只有它是零:

DissertationAppDelegate *appDelegate = (DissertationAppDelegate *)[[UIApplication sharedApplication] delegate];
if(appDelegate.array == nil) {
    appDelegate.array = [[NSMutableArray alloc] init];
}
[appDelegate.array addObject:@"THIS IS WHAT I WANT TO ADD"];
//here save that appDelegate.array to file
[appDelegate saveArray];

同时创建新的saveArray方法(您可以在其他地方再次使用它)将该数组保存到文件中:

- (void)saveArray {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *fullFileName = [NSString stringWithFormat:@"%@/arraySaveFile", documentsDirectory];
    [self.array writeToFile:fullFileName atomically:NO];
}