0

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!

mreynol
  • 309
  • 3
  • 17

1 Answers1

1

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!!

Kostis
  • 953
  • 9
  • 21
  • Plus i'm not sure but since I'm using Xcode 4.2 i think i need a segue ID? Also, I'm getting an error: Automatic Reference Counting Issue: No known class method for selector 'dequeueReusableCellWithIdentifier:forIndexPath:' – mreynol Nov 03 '12 at 01:16
  • The error is because you're coding for ios versions prior to ios6. Add this: UITableViewCell *cell = [[UITableViewCell alloc] init]; if (SYSTEM_VERSION_LESS_THAN(@"6.0")) { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; } else { cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; } and change [self presentViewController:tvc animated:YES completion:nil]; to this: [self presentModalViewController:tvc animated:YES]; – Kostis Nov 03 '12 at 01:19
  • Acually, i'm sorry but if you wouldn't mind editing your code above with that i would appreciate it. I'm getting all kinds of errors with the code in your comment. Thank you so much! – mreynol Nov 03 '12 at 01:34
  • Ooooh sorry!!! I just see that you use xcode 4.2 so just remove the ios 6 if statement! You will get an error otherwise! Just keep this: UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; – Kostis Nov 03 '12 at 01:51
  • okay, that all worked now...no errors. But i think i'm still missing something with the segue or something? I'm not sure what you mean by this? DON'T forget to set the Storyboard ID to identity inspector on storyboard for your tableViewController!! – mreynol Nov 03 '12 at 01:57
  • I assume you are going back to the tableViewController using segues. The storyboard id is used in this line: TestTableViewController *tvc = (TestTableViewController*)[storyboard instantiateViewControllerWithIdentifier:@"TestTableViewController"]; so xcode can understand in which controller you are reffering to. – Kostis Nov 03 '12 at 02:06
  • I keep learning to and i consider myself a starter yet! :) Personally, i think it's better to have a dynamic table. You just declare an array in viewDidLoad with the "static content" you want and then you fill the cell rows with the content of the array. If you want to edit a row you do something like this: if (indexPath.row==2){ cell.textLabel.text = @"whatever you want here"; } – Kostis Nov 03 '12 at 02:30
  • But I have several sections in my table. Each section has 1 or 2 cells that when tapped, open their own modelview. I even have two cells with TextFields. So, it doesn't seem like i should have a dynamic table, right? – mreynol Nov 03 '12 at 03:36
  • Okay, so i set it to dynamic. I have my table cell showing. Since I'm using a storyboard, don't i still have to create a segue between the TVC and my modelview? – mreynol Nov 03 '12 at 03:56
  • So, i have it all hooked up but its crashing when i hit the done button. I have a navigation controller embedded in the modalview. i know i must be missing something lame! – mreynol Nov 03 '12 at 04:07
  • I made a simple project to have a better picture! Adjust it to your needs: https://www.dropbox.com/s/lp9qpdhjgkgf7zk/passData.zip – Kostis Nov 03 '12 at 11:22
  • Thank you so much! Man, I don't understand though and perhaps its because i'm using older version 4.2 but I'm getting errors and warnings all over and i can't open the storyboard. It wants me to synthesize 'string' and 'textview'. But when i try to, it gives me error. It also doesn't recognize _string and _textview. Is it because you probably created with 4.5? I can remove the "_" in front of string and textview but it still won't work because it won't let me synthesize. Sorry to be such a pain! – mreynol Nov 03 '12 at 15:07
  • Nevermind, i haven't had enough coffee. I managed to synthesize and fix the errors and warnings..but the storyboard is the problem because you have later version. :( – mreynol Nov 03 '12 at 15:17
  • Its so strange! It works on present but not dismiss? I am also using a tabbarcontroller...so i just need it to dismiss and pass the data and still see my tabbarcontroller. – mreynol Nov 04 '12 at 22:34
  • Hello, i will review that article, but i wanted to let you know that i ended up using NSUserdefaults and it worked perfectly!!! I'm going to mark your answer as the answer but to those who are trying to do same thing...use NSUserDefaults! – mreynol Nov 05 '12 at 14:42