如何使用 NSUserDefault 将选定的 TableViewCell 文本发送到上一个 ViewController

How to use NSUserDefault to send selected TableViewCell text to previous ViewController

我的第一个 UIViewController 中有一个带有两个原型单元格的静态 UITableView。在第一个单元格中有一个带有 "select your country" 的标签。当用户点击该单元格时,它会转到另一个包含国家/地区的 UITableViewController。当用户 select 一个国家时,在第一个视图中标签文本应该更新为 selected 国家名称。为此,我必须将第二个 UIViewController 中的 selected 数据传递给第一个视图控制器。我希望使用 NSUserDefaults 来做到这一点。这是我的第二个带有表视图的视图控制器。

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


}

如果您提供可理解代码的答案,我将不胜感激,因为我是 iOS 的新手。

您可以在 First View Controller 中使用此代码从 NSUserDefault 获取值。

NSString *selectedPlace = [[NSUserdefaults standardUserDefaults] objectForKey:@"selectedplace"];

首先 - 您不需要在 didSelect 方法中添加太多代码来获取您选择的字符串。只需使用您的数组:

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

当你回到你的主人身边时(第一次)ViewController你需要更新你的TableView,因为数据已经改变了。所以,添加 viewWillAppear(如果你没有)方法并刷新你的 table:

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

然后,在您的 cellForRow 方法中:

...
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;
}

NSUserDefaults 不应用于应用程序状态。这是针对用户偏好设置的,应用程序关闭时应保留这些偏好设置。

正确的方法是在视图控制器之间直接传递数据:

正如@chedabob 所建议的那样,NSUserDefaults 不应用于维护应用程序状态。

相反,您可以使用 Protocols 在 First View Controller 中反向传播选择。

在你的情况下,只需使用

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

FirstViewControllerviewWillAppear 中更新字符串。

您应该使用委托模式来通知第一个视图控制器有关所选国家/地区的信息。为此,您必须在第二个视图控制器中编写协议。

@protocol SecondViewControllerDelegate

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

@end

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

之后,在第一个视图控制器中,您实施此方法来更新值。