Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-03-2011, 06:03 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 2
iambounty is on a distinguished road
Angry NSMutableArray in AppDelegate addObject from an IBAction

Hello all.
I've been trying to add a simple bookmarks facility to my app for my dissertation at uni. Im new to iPhone development so really I don't understand whats happening.

What im trying to do is add an object (string) to the array but what I've done doesnt work at all.

I got my array set up in my AppDelegate and it populates a table view in the BookmarksViewController.m file.

My application consists of a tab bar and navigation bar but I've chopped all that out here to make it easier to read.

Here is the code:

SETTING UP THE ARRAY

DissertationAppDelegate.h
Code:

#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


Code:
#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];
}
POPULATING THE TABLE VIEW WITH THE ARRAY

BookmarksViewController.h

Code:
@interface BookmarksViewController : UITableViewController {
	NSMutableArray *bookmarksArray;
}

@property (nonatomic, retain) NSMutableArray *bookmarksArray;
BookmarksViewController.m

Code:
#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];
}
PERFORMING THE IBACTION FROM ANOTHER VIEW CONTROLLER

Header File

Code:
-(IBAction) pushChap01Bookmark:(id)sender;
Implementation File

Code:
-(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.... So when you push the button I wanted it to add to the array in the app delegate and push the BookmarksViewController onto the navigation stack, and update the table. It does everything except for adding the object to the array.

If there is anyone out there who knows whats wrong then can you please help me out. I will appreciate it very much.

Thank you
iambounty is offline   Reply With Quote
Old 01-03-2011, 06:30 AM   #2 (permalink)
Fly-by-night Innovator
 
Join Date: Jun 2010
Posts: 364
musicwind95 is on a distinguished road
Default

Unless you have to, don't do the setup in the app delegate. Instead, in your viewDidLoad method, check to see if the file exists first:

NSFileManager fm = [NSFileManager defaultManager];
if (fm fileExistsAtPath:fullFileName)
// Load data
else
// Create new array


Make sure to use NSString's stringByAppendPathComponent: method. It's better guaranteed to generate a valid path string.

Finally, make sure to use your setter:
self.bookmarksArray = array; Otherwise, at the very least bookmarksArray will point to array, so anything that happens to array will also happen to bookmarksArray. This could lead to odd issues later on.
__________________
If I have helped you, please consider donating. I use PayPal. It would mean a lot to me!


For more iOS Development, check out my blog—Cups of Cocoa. All visitors welcome, but especially beginners!

Hope I have helped!

Please check out IceFall, a new action game available in the App Store!

Last edited by musicwind95; 01-03-2011 at 06:32 AM. Reason: Use your property (that [I]retain[/I]) means something.
musicwind95 is offline   Reply With Quote
Reply

Bookmarks

Tags
arrays, iphone, nsmutablearray

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 359
7 members and 352 guests
blueorb, fredidf, iAppDeveloper, iGamesDev, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:30 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0