Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone add to NSMutableArray from another viewController
    text
    copied!<p>I have a bit of a dilemma and I was wondering if the good folk here could lend me their programming expertise. I'll try to be as simple and precise as I possibly can so here goes:</p> <p>I'm a new IOS developer and have only been learning for a couple of months. I am developing an iPhone application for my dissertation at university. The app is simply a guide for people who wish to develop for the iPhone themselves, consisting of tutorials. It consists of numerous Table Views but there is one thing that has got me stumped.</p> <p>What im trying to do: One feature im trying to include in my app is a bookmarks facility, this will be accessible from a tab bar. I want to be able to click on a button from any nib file (tutorial) which adds a string to an existing NSMutableArray. This string will correspond with the name of the tutorial where the IB-Action was performed and after added to the Array I can load the nib file when selecting the row at index path.</p> <p>The Problem: I can add any object to the array from within the implementation file that contains the array but cannot figure out how to add it from a different implementation file. The UITable view populates from the array perfectly but adding a new entry is another story.</p> <p>I'll show you my code but i'll leave out anything that is unrelated.</p> <p><strong>BookmarksViewController.h</strong></p> <pre><code>@interface BookmarksViewController : UITableViewController { NSMutableArray *bookmarksArray; } @property (nonatomic, retain) NSMutableArray *bookmarksArray; @end </code></pre> <p><strong>BookmarksViewController.m</strong></p> <pre><code>-(void)viewDidLoad { bookmarksArray = [[NSMutableArray alloc] init]; NSLog(@"String Added"); [bookmarksArray addObject:@"String"]; [super viewDidLoad]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Configure the cell... cell.textLabel.text = [bookmarksArray objectAtIndex:indexPath.row]; return cell; } -(void)dealloc { [bookmarksArray release]; [super dealloc]; } </code></pre> <p>NOW FOR THE SECOND VIEW CONTROLLER</p> <p><strong>Ch01GettingStarted.h</strong></p> <pre><code>@interface Ch01GettingStarted : UIViewController { IBOutlet UIScrollView *ScrollView; } -(IBAction) pushChap01Bookmark:(id)sender; @end </code></pre> <p><strong>Ch01GettingStarted.m</strong></p> <pre><code>-(IBAction) pushChap01Bookmark:(id)sender{ BookmarksViewController *bookmarksViewController = [[BookmarksViewController alloc]init]; [bookmarksViewController.bookmarksArray addObject:@"NewString"]; NSLog(@"ADD ENTRY"); [bookmarksViewController.tableView reloadData]; NSLog(@"RELOAD TABLE"); [bookmarksViewController release]; NSLog(@"ADD BOOKMARK RELEASE"); } </code></pre> <p>BTW - the IB-Action was declared in the header file.</p> <p>Ah, i originally tried doing this as '[BookmarksViewController.bookmarksArray addObject:@"NewString"];' but I came up with an "expected ':' at '.'" error and I read somewhere that I needed to use an instance variable of BookmarksViewController so i declared it just above the addObject method.</p> <p>Please be gentle with me as I haven't been doing this for long but this is certainly something that's going to be a big part of my professional future.</p> <p>Any insight anyone could offer to me would be magical. </p> <p>Thank you, thank you, thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload