views:

269

answers:

2

I have a simple UITableViewController in a UINavigationController that displays a list of strings from an array with the default Edit/Done button on the right-hand side of the navigation bar.

When pressing the Edit button, the UITableView animates correctly and shows the red minus icons to delete. Pressing the delete button removes the row from the table view and the array (implemented in the tableView:commitEditingStyle:forRowAtIndexPath: method of the UITableViewController).

I would now like to allow the user to add a row to the view (and add the string to the underlying array), but I'm not sure how to go about doing so. The commitEditingStyle method has else if (editingStyle == UITableViewCellEditingStyleInsert), but I don't know how I can get the user to input the string.

I've read the Table View Programming Guide (more specifically the example of adding a table-view row), but this seems to require a whole new UIViewController subclass just to get a string from the user.

Is there no easier way?

A: 

You could use a UIAlertView or similar class yourself. Just pop up the modal view to request the string, establish the right callbacks, then pop it in your dataSource.

You can also insert a cell with a UITextView and a "Tap to Edit" placeholder, then on the textView Callbacks, remove the textView and display the string. Further editing would need to drill down or do something else

coneybeare
+2  A: 

Creating another view controller is probably going to be the easiest way in the long run. You can present it modally by calling

SomeViewController* theViewController = [[SomeViewController alloc] init];
[self presentModalViewController: theViewController animated: YES];
[theViewController release];

When the theViewController is ready to go away it can call

[[self parentViewController] dismissModalViewControllerAnimated: YES];

OR you can setup a protocol for your new view controller so it can notify your original view controller of completion and send a value back, if you wanted an NSString back you might use

@protocol MyViewControllerDelegate
- (void)myViewControllerDelegate: (MyViewController*)myViewController didFinishWithValue: (NSString*)theString;
@end

MyViewController would then have a delegate property

@interface MyViewController
{
    id<MyViewControllerDelegate> delegate;
}
@property(nonatomic,assign) id<MyViewControllerDelegate> delegate;
@end

If you use the protocol method your original view controller will adopt that protocol and will dismiss the modal view itself when it receives this message.

I hope that helps out, it may seem a little complicated at first, but it makes gathering data very easy.

jessecurry