我的第一个 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/