1

So I have seen a few tutorials out there on how to do this but really they all involve hard-coding buttons and text fields in. What I have done is created a view controller A, with an embedded table view. And in the navigation bar I have added a button (the "Add") plus symbol button. This button I have made its action to modally bring up another view controller B where I have a text field and a button Save.

Here is a picture for reference: enter image description here

So what I want to do, is have the user be able to press the green plus button in view controller A, and then it will bring up view controller B where I can enter a username, press save, and it will dismiss itself (I know how to do this) and reveal A again but now showing the newly added contact.

I am using an NSMutableArray to store and populate the table. What is the simplest, and easiest way in which I can do this, without having to hardcode buttons in as shown in the apple docs Table View Programming Guide. I have two separate classes for A I have the Contacts class and for B I have designated it the addContact class. Both classes are simply view controller subclasses.

E.Cross
  • 2,087
  • 5
  • 31
  • 39
  • Does anyone have an answer for this? I really need help and so far none of these answers explain what I want to do. – E.Cross Nov 04 '13 at 07:37
  • Anyone? This has been bugging me for a long time. Would really appreciate some help on this! – E.Cross Nov 07 '13 at 04:16

2 Answers2

0

First I would push the second view instead of presenting it modally. To the Segue (connector between first and second view) you can also attach an identifier.

https://developer.apple.com/library/ios/recipes/xcode_help-interface_builder/articles-storyboard/StoryboardSegue.html

Additionally you can perform some action in the prepareForSegue method before the view is pushed. In this method I would then pass the array and tableview, such that you can add the username and reload the table when the save button is pressed.

http://nscookbook.com/2013/01/ios-programming-recipe-5-passing-values-between-segues-with-prepareforsegue/

Nils
  • 1,750
  • 14
  • 10
0

The scope of your question isn't very clear, but assuming you know how to pass the saved value back to the parent view controller, you would insert the table row using something like this:

- (void)addItem:(id)item { // where item is the object being added to the data model
    [self.items addObject:item]; // where self.items is your mutable array
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.items.count - 1 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

The insertRowsAtIndexPaths:withRowAnimation: method informs the table view that you've added new items to your data model at the given index paths and it will then call your data source methods, including cellForRowAtIndexPath, to generate the new cell and animate it into place.

Timothy Moose
  • 9,895
  • 3
  • 33
  • 44
  • I am confused at the `[self.tableView...` I get no option to use this method in my view controller `B` (the one which allows you to enter the username and save). Is there anything I can help clear up that is confusing in my question? – E.Cross Nov 02 '13 at 22:16
  • The above code would live in view controller A. When you tap the save button, view controller B should notify view controller A. Then view controller A would get the value to be added to the table and dismiss view controller B. The communication between view controllers would typically either be done with a delegate protocol or a callback block. – Timothy Moose Nov 02 '13 at 22:39
  • What do you mean by 'notify'? Can you be more explicit or provide code example? – E.Cross Nov 03 '13 at 01:39
  • View controller B needs to pass the value back to view controller A (i.e. notify view controller A). It is then view controller A's job to dismiss B and insert the value into the table. Take a look at this question http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Timothy Moose Nov 07 '13 at 15:19