3

I have two UIViews in my app. in first view there is a tableveiw with two cells(to select city and country). when user select first cell(to select city), then it goes to anothrview that has a list of cities. then when user select a city(select a tableviecell text), the selected should display in firtview's tableviewcell text. this is my code in secondview controller(it is a tableview Controller).

- (void)viewDidLoad {

    [super viewDidLoad];

    detailFlights = @[@"colombo1",@"colombo2",@"colombo3",@"colombo14",@"colombo15",@"colombo16",@"colombo17"];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identi" forIndexPath:indexPath];

    cell.textLabel.text = [detailFlights objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
caldera.sac
  • 4,918
  • 7
  • 37
  • 69
  • you should implement [Delegate](http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c) – MD. Jan 02 '16 at 09:47
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) –  Jan 02 '16 at 10:06

5 Answers5

0

This kind of problem is solved using the delegate pattern. A pattern widely used in iOS programming. See this question if you don't know how it works.

Community
  • 1
  • 1
deadbeef
  • 5,409
  • 2
  • 17
  • 47
0

As deadbeef said, you must implement delegate method to transfer data to first view, but logic for that is to choose objectAtIndex of indexPath.row in didSelect: [detailFlights objectAtIndex:indexPath.row];

Stefan
  • 1,283
  • 15
  • 33
0

In didDeselectRowAtIndexPath method you can get that string using following way

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSString *selectedString = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
}
  1. now save to pass previous view controller you can store it in UserDefaults OR
  2. make a property in previousViewController and before go back set it value like follow

if you are using navigationController then

ViewController *previousViewController = (ViewController *)   self.navigationController.viewControllers[self.navigationController.viewControllers.count-2];
previousViewController.selectedString = selectedString;

or

  1. you can also use block for passing data. example of block
Community
  • 1
  • 1
Jaydeep Patel
  • 1,699
  • 17
  • 30
0

As an alternative to the delegate method that others have mentioned, you can use an unwind segue to pass data back to the previous view controller.

Unwind segues give you a way to "unwind" the navigation stack back through push, modal, popover, and other types of segues. You use unwind segues to "go back" one or more steps in your navigation hierarchy. Unlike a normal segue, which create a new instance of their destination view controller and transitions to it, an unwind segue transitions to an existing view controller in your navigation hierarchy. Callbacks are provided to both the source and destination view controller before the transition begins. You can use these callbacks to pass data between the view controllers.

Here's a example where the second (source) view controller presented a list of fonts, and the first (destination) view controller updates its table row to show the selected font:

- (IBAction)unwindFromFontPreference:(UIStoryboardSegue *)segue
{
    FontTableViewController *fontTableViewController = segue.sourceViewController;
    if (self.currentFontSelection != fontTableViewController.currentFontSelection)
    {
        self.currentFontSelection = fontTableViewController.currentFontSelection;
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:LALSettingsTableViewSectionFont]]
                              withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
0

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()<UITableViewDelegate, UITableViewDataSource, ViewControllerDelegate>

@property (nonatomic, retain) NSMutableArray* data;
@property (nonatomic, retain) IBOutlet UITableView* tableView;
@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.data = [NSMutableArray array];
    [self.data addObject:@"country"];
    [self.data addObject:@"city"];
    // Do any additional setup after loading the view, typically from a nib.
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    NSString* text = [self.data objectAtIndex:indexPath.row];

    cell.textLabel.text = text;
    return cell;
}

-(void) updateText:(NSString *)text
{
    [self.data replaceObjectAtIndex:1 withObject:text];
    [self.tableView reloadData];
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"secondView"])
    {
        UINavigationController* controller = [segue destinationViewController];
        NSArray *viewControllers = controller.viewControllers;
        SecondViewController* viewController = [viewControllers objectAtIndex:0];
        viewController.delegate = self;
    }
}

SecondViewController.h

#import <UIKit/UIKit.h>

@protocol ViewControllerDelegate <NSObject>

-(void) updateText:(NSString*)text;

@end
@interface SecondViewController : UIViewController

@property (nonatomic, assign) id<ViewControllerDelegate> delegate;
@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@property(nonatomic ,retain) NSArray* detailFlights;
@end

@implementation SecondViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.detailFlights = @[@"colombo1",@"colombo2",@"colombo3",@"colombo14",@"colombo15",@"colombo16",@"colombo17"];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.textLabel.text = [self.detailFlights objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{   NSString* text = [self.detailFlights objectAtIndex:indexPath.row];
    [self.delegate updateText:text];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

whole project is here

hariszaman
  • 8,202
  • 2
  • 40
  • 59