0

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:

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.

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.

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.

I'll show you my code but i'll leave out anything that is unrelated.

BookmarksViewController.h

@interface BookmarksViewController : UITableViewController {
 NSMutableArray *bookmarksArray;
}

@property (nonatomic, retain) NSMutableArray *bookmarksArray;
@end

BookmarksViewController.m

-(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];
}

NOW FOR THE SECOND VIEW CONTROLLER

Ch01GettingStarted.h

@interface Ch01GettingStarted : UIViewController {
 IBOutlet UIScrollView *ScrollView;
}
-(IBAction) pushChap01Bookmark:(id)sender;

@end

Ch01GettingStarted.m

-(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");
}

BTW - the IB-Action was declared in the header file.

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.

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.

Any insight anyone could offer to me would be magical.

Thank you, thank you, thank you.

1
  • Quick tip: select the code in your question and press the '{ }' button in the toolbar. It will put the code in a block and change the font, making it a lot easier to read. Commented Jan 2, 2011 at 12:19

3 Answers 3

1

The problem lies in this method:

-(IBAction) pushChap01Bookmark:(id)sender

You are creating a NEW bookmarksViewController here, by doing this:

BookmarksViewController *bookmarksViewController = [[BookmarksViewController alloc]init];

You don't want to do that because you want to update the current view controller. You need to create a link from Ch01GettingStarted.

Assuming you are using Interface Builder, you could create this link using an IBOutlet. In the Ch01GettingStarted interface, add the following line:

IBOutlet BookmarksViewController *bookmarksViewController;

(Between the brackets)

I think you already know how to link this in Interface Builder.

Then just remove this line:

BookmarksViewController *bookmarksViewController = [[BookmarksViewController alloc]init];

And this line:

[bookmarksViewController release];

And it should work.

Why? The 'bookmarksViewController' variable now references the original object (the one you created in Interface Builder) that is actually displayed.

Sign up to request clarification or add additional context in comments.

9 Comments

Hi, thanks for your quick reply. I added ' IBOutlet BookmarksViewController *bookmarksViewController;' to Ch01GettingStarted.h but it comes up with "Expected specifier-qualifier-list before 'bookmarksViewController'" Why doesnt it like it? Sorry but im not too clear as to how to hook it up in interface builder either. I assume that I add a UIViewController and link it up, is this right or wrong? Thank you, ill keep trying.
Ok, i imported "BookmarksViewController.h" into the header file and the errors went away but when i click on the button the program crashes.
Very good you found that one out yourself! What is the crash error message? (You need to open the 'console' for that.)
What I did in interface builder was drag on a UIViewController, set its class and nib name to 'BookmarksViewController' and then dragged from files owner to the UIViewController and assigned the iboutlet. I ran from the console and when you press the button once it does nothing but when you press it again it says "Program received signal: “EXC_BAD_ACCESS”." Thank you.
The button is running through the action because the NSLogs are being printed out to the console. When I press the button the second time it just freezes and console reads "Program received signal: “EXC_BAD_ACCESS”."
|
0

Just to be sure. Do you have this method in BookmarksViewController.m?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  [bookmarksArray count];
}

1 Comment

I have -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [bookmarksArray count]; } thanks
0

Because after bookmarksViewController is created. ViewDidLoad method not run yet. bookmarksArray has not been created. So bookmarksArray = nil. You can't add object to nil object. In Second Viewcontroller, You should create bookmarksViewController in loadView method. And addobject in pushChap01Bookmark.

I'm not good English. I hope you can understand it. :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.