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