0

Already manage to pass input text from first VC to second VC with tableview. First VC do not have tableview.

First VC: UItextField - user type some name. UIButton *add - button with segue (prepareForSegue)

Second VC: TableView displaying input text from first VC with prepareForSegue

Question: Tableview displays only one row at the time, so when i click back to input another name, and click add buton, tableview obviously gets reset and does not remember first input text. So how get tableview to remember names and put it in other rows. I don't know should i type code in prepareForSegue, or make delegate in first VC. Please explain in detail. Thank you alot.

Igy
  • 129
  • 1
  • 16
  • I the only problem is that the Second VC doesn't "remember" the input text, just add them to an array. – Mikael Jun 24 '14 at 13:01
  • input text/prepareForSegue/SecondVC/NSMutableArray/myCell.textLabel.text = *array* [indexPath.row]; - that's a pathway of input text, already have input text in NSMutableArray – Igy Jun 24 '14 at 13:59
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Hot Licks Jun 24 '14 at 14:35

2 Answers2

0

I believe there are many ways to achieve this, if you want to persist data maybe core Data is your best bet, however if its just a simple logic then I suggest you using delegates.

ViewController

Interface

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *dataTextField;
@property (nonatomic) NSMutableArray *items;
- (IBAction)AddData:(id)sender;
@end

Implementation

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _items = [[NSMutableArray alloc]init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)AddData:(id)sender {
    if ([self.dataTextField.text length]> 0) {
        [_items addObject:self.dataTextField.text];
        [self performSegueWithIdentifier:@"tableSegue" sender:self];
    }else{
       UIAlertView *alertView =  [[UIAlertView alloc]initWithTitle:@"Error"
                                                           message:@"You must enter some data"
                                                          delegate:self
                                                 cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
    }

}




- (void)addItemViewController:(TableViewController *)controller didFinishSelectingItem:(NSMutableArray *)item selectedTag:(int)tag{
    NSLog(@"DATA=%@", item);
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"tableSegue"]){
        TableViewController *controller = (TableViewController *)segue.destinationViewController;
        controller.items = _items;
    }
}
@end

TableViewController

Interface:

@protocol TableViewControllerDelegate <NSObject>
- (void)addItemViewController:(id)controller didFinishSelectingItem:(NSMutableArray *)item selectedTag:(int)tag;
@end

@interface TableViewController : UITableViewController
@property (nonatomic, weak) id <TableViewControllerDelegate> delegate;
@property (nonatomic) NSMutableArray *items;
@end

Implementation

@implementation TableViewController

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.items count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [self.navigationController popViewControllerAnimated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [[cell textLabel] setText:_items[indexPath.row]];
    return cell;
}

@end
meda
  • 45,103
  • 14
  • 92
  • 122
  • meda, where i can buy a dinner? :), tried with your code and with some tweaking it works, i learn alot thanx to you about delegates, thank you – Igy Jun 24 '14 at 19:58
  • i did aprove it, but i have another question, i know now i'm beginning to be pain in the a**, this is last question i promise :), for displaying rows in reverse, (on top) i have to reverse objects in array, i know how to do that, but is there easier way? thank you – Igy Jun 24 '14 at 20:56
  • Thanks, @keewee23 to reverse the order, put this line in viewdidLoad `dataSourceArray = [[_items reverseObjectEnumerator] allObjects];`. no problem this is a Q&A site, if you ever need me again just leave a comment here – meda Jun 24 '14 at 21:08
  • thank you alot, with little tweaks i got it working with this `_items = [[[_items reverseObjectEnumerator] allObjects] mutableCopy];` :)) – Igy Jun 24 '14 at 21:48
  • meda, when objects are reversed, i delete cells in tableview, on screen they are deleted, but when i go back and input another text, deleted cells appear again with that new text, weird, will try to google it – Igy Jun 25 '14 at 01:32
0

View controllers are part of the controller layer of your application. They do not do "business logic" — heavy processing or more-persistent storage of data, whether to disk or just for that session.

The model section of your application handles that. Each view controller gets and sets data via the model. There should be no ongoing conversations between view controllers*; anything beyond things you would specify to an init is indicative of a broken design.

So you're asking the wrong question.

You would have a model that somehow vends the items that should go into the first view controller. You will have a second view controller that knows how to edit one item. The level of communication from first to second will be "this is the item you should be editing".

It is the responsibility of the first view controller and the model to ensure that it can keep its display up to date. The second view controller is responsible only for modifying its record. It shouldn't need to communicate anything whatsoever to the first view controller.

Whether you do that by pulling results from the model on every viewWillAppear, by some sort of live observation, by notifications emanating outward from the model or by some other means entirely doesn't matter.

(* subject to caveats where you've used containment, e.g. changes to the title that a view controller has but which is shown by a navigation controller are technically an ongoing conversation)

Tommy
  • 99,986
  • 12
  • 185
  • 204