ios - 如何使用 NSUserDefault 将选定的 TableViewCell 文本发送到之前的 ViewController

标签 ios objective-c uitableview nsuserdefaults

我的第一个 UIViewController 中有一个带有两个原型(prototype)单元格的静态 UITableView。在第一个单元格中有一个带有“选择您的国家/地区”的标签。当用户点击该单元格时,它会转到另一个包含国家/地区的 UITableViewController。当用户选择一个国家时,在第一个 View 中,标签文本应该更新为所选的国家名称。为此,我必须将第二个 UIViewController 中的选定数据传递给第一个 View Controller 。我希望使用 NSUserDefaults 来做到这一点。这是我的第二个带有 TableView 的 View Controller 。

@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 新手。

最佳答案

NSUserDefaults 不应用于应用程序状态。它适用于用户偏好,在应用关闭时应该保留。

正确的方法是在 View Controller 之间直接传递数据:https://stackoverflow.com/a/9736559/78496

关于ios - 如何使用 NSUserDefault 将选定的 TableViewCell 文本发送到之前的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34575010/

相关文章:

iphone - identifierForVendor 与 SecureUDID/OpenID

objective-c - 如何将 nib 自定义 View 绑定(bind)到 NSVIew 子类

ios - Xcode 6 在 IB 中添加 tableHeaderView

ios - 从另一个类/方法调用委托(delegate)方法(特别是 didSelectRowAtIndexPath)

ios - IBAction 未作为选项出现

ios - 为什么这些约束在动画时似乎滞后?

ios - Facebook 请求 FBWebDialog UI 看起来很奇怪

ios - 检测将在 UIActivityViewController 中选择哪个事件

ios - 使用主从模板和核心数据时,如何在导航 Controller 之前添加 uiviewcontroller

ios - cellForRowAtIndexpath 没有被调用