0

Currently I have a UITableViewController which displays a list of items from an array within that controller implementation file. Now, I would like to have a button on the top right, which, when you press it, brings you to a simple page in which you can add an item to the list on the previous page. When you are done adding the item, it just brings you back to the list, which now has one more item in it.

So, I have a navigationController leading into my tableViewController and a button which "pushes" you to the add item page.

How do I access a reference to the tableViewController, so that I can call my -(void)addNewItem:(NSString *) name; method?

GusP
  • 2,454
  • 2
  • 23
  • 32
Des
  • 1

2 Answers2

1

Here's a basic overview of how I've done this. It's worked for me but I'm not sure it's the best way--others will surely chime in.

The "list page" (your first UITableViewController) implements a delegate that the "add page" calls into to let the "list page" know the details of the item to add.

When the "list page" gets called back by the "add page" with said details, it saves the new item and then closes the "add page".

To get this all hooked up, when the "list page" creates and displays the "add page", it passes self to an ivar on the "add page" (that I call delegate). That's basically how the two get linked and communicate.

Here's a protocol I define for the delegate:

@protocol AddItemViewControllerDelegate
- (void)addItemViewController:(AddItemViewController *)controller 
             withNewEventName:(NSString *)eventName;
@end

Here's the "list page" button click handler:

- (IBAction)addItem
{
    AddItemViewController *controller = [[AddItemViewController alloc] initWithNibName:@"AddItemView" bundle:nil];
    controller.delegate = self;
    [self presentModalViewController:controller animated:YES];
    [controller release];
}

Here's the commit code from the "add page":

if ([self.delegate respondsToSelector:@selector(addItemViewController:withNewEventName:)]) {
    [self.delegate addItemViewController:self withNewEventName:eventNameTextField.text];
}

Finally, here's the implementation of the delegate protocol in the "list page":

- (void)addItemViewController:(AddItemViewController *)controller 
             withNewEventName:(NSString *)eventName 
{
    EventModel *newEvent = [[EventModel alloc] init];
    newEvent.name = eventName;
    [eventsList addObject:newEvent];
    [newEvent release];
    [self saveEvents];
    [self dismissModalViewControllerAnimated:YES];
}
GusP
  • 2,454
  • 2
  • 23
  • 32
  • Happy to help. If this solved your problem, please mark it as the answer. – GusP Jun 25 '12 at 16:30
  • In trying to implement this, I have run into some conceptual issues. Is the protocol written in the "list" or "add" page? – Des Jun 28 '12 at 00:08
  • The list page is the one that implements the protocol. When you create and show the "add page", you pass a reference to "self" so that you can call back into the "list page" instance through the protocol implementation. Apologies for brevity, I'm on a cruise with just a phone and a horrible net connection. – GusP Jun 28 '12 at 15:09
  • No problem, thanks your help. I am still having trouble... when I use the [self presentModalViewController:controller] method I get an error that says: NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle'. Any idea what this is? – Des Jun 29 '12 at 17:27
  • I haven't encountered that exception myself but from looking around a bit it seems a bit tricky to figure out. There are several other questions on SO on this topic that may help since such as [this one](http://stackoverflow.com/questions/10792114/nsinternalinconsistencyexception-could-not-load-nib-in-bundle), [another one](http://stackoverflow.com/questions/5415252/nsinternalinconsistencyexception-could-not-load-nib-in-bundle), [and another](http://stackoverflow.com/questions/5099707/could-not-load-nib-in-bundle-nsbundle). Good luck though! – GusP Jul 03 '12 at 00:01
-1

I had the same problem plus new item has to be added to local database. The question in your case is what is the data source of your table view? Is it coredata,SQLLite or just an array of items? I found solution with singleton class here is tutorial . This will take some time you to implement singleton class though.

If you are going to add only text to your table view you can use alertview.

For example if you are using array as datasource of your table you can add a single item with alert view each time when you press the button you have created

frist create a button connect your button to do IBAction in your .xib file using file owner

-

(IBAction)add:(UIButton *)sender
{

    UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter Item Name"
                                                     message:@"  "   
                                                    delegate:self 
                                           cancelButtonTitle:@"Cancel"
                                           otherButtonTitles:@"OK", nil];

    UITextField *nameField = [[UITextField alloc] 
                              initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
    [nameField setBackgroundColor:[UIColor whiteColor]];
    nameField.text = @"";
    nameField.tag = 100;
    [dialog addSubview:nameField];
    [nameField release];
    [dialog show];
    [dialog release];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    yourArraylist = [[NSMutableArray alloc] init];


    if([title isEqualToString:@"OK"])
    {

        UITextField* nameField = (UITextField *)[alertView viewWithTag:100];
        [yourArraylist addObject:nameField.text];

     // do additional  data source issues here 
    [self.mytableView reloadData];
}
SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
  • Thanks! I will be storing items in the table, so alertview won't fit, but I will take a look at that tutorial. – Des Jun 25 '12 at 03:12