简体   繁体   中英

Objective C - Passing data from second VC with tableview to first VC

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.

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

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, eg changes to the title that a view controller has but which is shown by a navigation controller are technically an ongoing conversation)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM