ios - 如何将选定的表格 View 单元格文本发送到 iOS 中的上一个 View Controller ,Objective C(向后传递数据)

标签 ios objective-c uitableview

我的应用程序中有两个 UIView。 在第一个 View 中,有一个带有两个单元格的表格(用于选择城市和国家/地区)。当用户选择第一个单元格(选择城市)时,它会转到另一个包含城市列表的 View 。然后当用户选择一个城市(选择一个 tableviecell 文本)时,所选内容应显示在 firtview 的 tableviewcell 文本中。 这是我在 secondview Controller 中的代码(它是一个 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];
}

最佳答案

这类问题使用委托(delegate)模式解决。 iOS 编程中广泛使用的一种模式。参见 this question如果您不知道它是如何工作的。

关于ios - 如何将选定的表格 View 单元格文本发送到 iOS 中的上一个 View Controller ,Objective C(向后传递数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34564388/

相关文章:

ios - 从 iOS 应用程序发推文时卡片不显示包含图像

ios - AFNetworking 2.0成功 block 主线程

iphone - iOS/Obj-C UIScrollView 和 PageControl 不工作

ios - UITableView 重新加载时滚动动画

ios - UITableview 以编程方式设置多选

ios - UIView 圆角半径动画

ios - 正确的数据未显示在 ios 图表中

ios - 如何在 Swift 中使用协议(protocol)公开 Objective-C 对象的私有(private)类方法?

objective-c - Objective-C 中的 c++ 等价物 [alloc [init]]

ios - 从 UITableView 选择行时将核心数据对象传递给另一个 View Controller