Question

i am severely struggling with what seems like such a very simple thing: passing data from modal view to its parent. I have tried countless ways and to my surprise there is not much online and nothing seems to match what i'm trying to do. I'm using storyboards and most examples out there do not use seques/storyboards.

If anyone could possibly provide some sample code for me i would tremendously appreciate it!

I have a static table view controller with a custom cell. Within the cell I have a label. I want to tap on the cell the present the modal view with a textview in it. Type data into the text view, then hit a save/done button to dismiss the modal view and have that data appear on my UILabel in my cell.

I know it must sound very simple and there are many questions on here about it but nothing does this very thing. Some sample code would be so appreciated. I'm completely stuck on building my app and can't go any further until i get this!

Was it helpful?

Solution

You can try NSUserDefaults to store your data from textview and then read them back for the label.

EDIT: If you don't want to use NSUserDefaults as it's not the "right" way (but the easy one) you can try this:

In your tableViewController.h create a NSString:

#import <UIKit/UIKit.h>
@interface TestTableViewController : UITableViewController
@property (nonatomic, strong) NSString *string;
@end

In your viewController.h that contains the textView add these:

- (IBAction)doneButton;
@property (weak, nonatomic) IBOutlet UITextView *textView;

In viewController.m:

- (IBAction)doneButton {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
TestTableViewController *tvc = (TestTableViewController*)[storyboard instantiateViewControllerWithIdentifier:@"TestTableViewController"];
tvc.string = _textView.text;
[tvc setModalPresentationStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:tvc animated:YES];
// ios6 version:
//[self presentViewController:tvc animated:YES completion:nil];}

Then back in your tableViewController.m display the input data to your cell (you don't need to use a label):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{      
static NSString *CellIdentifier = @"test";
UITableViewCell *cell = [[UITableViewCell alloc] init]; 
if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { 
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; } 
else { 
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
} 
 // Configure the cell...
cell.textLabel.text = _string;

return cell;}

DON'T forget to set the Storyboard ID to identity inspector on storyboard for your tableViewController!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top