0

I have a static UITableView with two prototype cells in my first UIViewController. in the first cell there is a label with "select your country". When user tap that cell, it goes to another UITableViewController that includes countries. When user select a country, in the first view label text should update with the selected country name. To do that I have to pass the selected data in second UIViewController to first view controller. I hope to use NSUserDefaults to do that. this is my second view controller with a tableview.

@implementation FlightfromTableViewController
{
    NSArray *detailFlights;
}

- (void)viewDidLoad {

    [super viewDidLoad];

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

}

#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];

    Pass *p = [Pass new];
    p.selectedString = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;

      NSString *selecteOne = p.selectedString;
      [[NSUserDefaults standardUserDefaults] setObject:selecteOne forKey:@"selectedplace"];
     [[NSUserDefaults standardUserDefaults] synchronize];


}

I appreciate if you provide answer with understandable code, because I'm new to iOS.

chedabob
  • 5,835
  • 2
  • 24
  • 44
caldera.sac
  • 4,918
  • 7
  • 37
  • 69

5 Answers5

2

NSUserDefaults should not be used for application state. It's for user preferences which should persist when the app closes.

The correct way is to pass your data directly between your viewcontrollers: https://stackoverflow.com/a/9736559/78496

Community
  • 1
  • 1
chedabob
  • 5,835
  • 2
  • 24
  • 44
2

As suggested by @chedabob, NSUserDefaults should not be used to maintain application state.

Instead, you can use Protocols to back propagate the selection in First View Controller.

In your case, just use

[[NSUserdefaults standardUserDefaults] objectForKey:@"selectedplace"];

in viewWillAppear of FirstViewController to update the string.

1

First of all - you don't need to much code in your didSelect method to get your selected string. Just use your array:

NSString *selectedText = [detailFlights objectAtIndex: indexPath.row]; // or detailFlights[indexPath.row]

And when you come back to your master (first) ViewController you need to update your TableView, because data has changed. So, add viewWillAppear (if you haven't one) method and refresh your table:

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear: animated];
  [self.tableView reloadData]; // put here name of your tableView's property
}

And then, in your cellForRow method:

...
if (indexPath.row == 0) { // in first row you store country
  NSString *text = @"Select country"; // default text
  NSString *selectedCountry = [[NSUserdefaults standardUserDefaults] objectForKey:@"selectedplace"];
  if (selectedCountry) { // if it exist
    text = selectedCountry;
  }
  cell.textLabel.text = text;
}
rkyr
  • 3,131
  • 2
  • 23
  • 38
  • nice, it works.but I got a small problem here. in my didselectrowatindexpath method.think I select first row in the second tableview. nothing happen.after I clicked another row.it works. any wrong with that method in my code. I'm testing with this simulator. ` NSString *selectedText = [detailFlights objectAtIndex:indexPath.row]; [[NSUserDefaults standardUserDefaults] setObject:selectedText forKey:@"st"]; [[NSUserDefaults standardUserDefaults] synchronize]; ` – caldera.sac Jan 03 '16 at 11:23
  • 1
    @Graham try move your code from didDeselect method to didSelect. – rkyr Jan 03 '16 at 12:11
0

You can get value from NSUserDefault by using this code in your First View Controller.

NSString *selectedPlace = [[NSUserdefaults standardUserDefaults] objectForKey:@"selectedplace"];
technerd
  • 14,144
  • 10
  • 61
  • 92
0

You should use delegate pattern to inform the first view controller about selected country. For this you have to write protocol in second view controller.

@protocol SecondViewControllerDelegate

-(void)secondViewController:(UIViewController *)vc selectedCountry:(NSString *)country;

@end

then in (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; delegate.secondViewController(self) selectedCountry:@"selectedCountry"; }

After that in first view controller you implement this menthod to update the value.