简体   繁体   English

如何将选定的tableview单元格文本发送到iOS中的前一个视图控制器,Objective C(向后传递数据)

[英]How to send selected tableview cell text to previous view controller in iOS, Objective C (passing data backward)

I have two UIViews in my app. 我的应用程序中有两个UIViews。 in first view there is a tableveiw with two cells(to select city and country). 在第一个视图中,有一个有两个单元格的表格(选择城市和国家)。 when user select first cell(to select city), then it goes to anothrview that has a list of cities. 当用户选择第一个单元格(选择城市)时,它会进入具有城市列表的anothrview。 then when user select a city(select a tableviecell text), the selected should display in firtview's tableviewcell text. 然后当用户选择一个城市(选择一个tableviecell文本)时,所选内容应显示在firtview的tableviewcell文本中。 this is my code in secondview controller(it is a tableview Controller). 这是我在secondview控制器中的代码(它是一个tableview控制器)。

- (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];
}

This kind of problem is solved using the delegate pattern. 使用委托模式解决了这种问题。 A pattern widely used in iOS programming. iOS编程中广泛使用的模式。 See this question if you don't know how it works. 如果你不知道它是如何工作的,请看这个问题

正如DEADBEEF所述,必须实现委托方法将数据传输到第一视图,但该逻辑是选择objectAtIndexindexPath.rowdidSelect[detailFlights objectAtIndex:indexPath.row];

In didDeselectRowAtIndexPath method you can get that string using following way 在didDeselectRowAtIndexPath方法中,您可以使用以下方式获取该字符串

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSString *selectedString = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
}
  1. now save to pass previous view controller you can store it in UserDefaults OR 现在保存以传递以前的视图控制器,您可以将其存储在UserDefaults OR中
  2. make a property in previousViewController and before go back set it value like follow 在previousViewController中创建一个属性,在返回之前设置它的值如下

if you are using navigationController then 如果你正在使用navigationController

ViewController *previousViewController = (ViewController *)   self.navigationController.viewControllers[self.navigationController.viewControllers.count-2];
previousViewController.selectedString = selectedString;

or 要么

  1. you can also use block for passing data. 您也可以使用块来传递数据。 example of block 块的例子

As an alternative to the delegate method that others have mentioned, you can use an unwind segue to pass data back to the previous view controller. 作为其他人提到的委托方法的替代方法,您可以使用展开segue将数据传递回上一个视图控制器。

Unwind segues give you a way to "unwind" the navigation stack back through push, modal, popover, and other types of segues. 展开segues为您提供了一种通过推送,模态,弹出和其他类型的segue“展开”导航堆栈的方法。 You use unwind segues to "go back" one or more steps in your navigation hierarchy. 您可以使用展开segues“返回”导航层次结构中的一个或多个步骤。 Unlike a normal segue, which create a new instance of their destination view controller and transitions to it, an unwind segue transitions to an existing view controller in your navigation hierarchy. 与正常segue不同,正常segue创建目标视图控制器的新实例并转换为它,展开segue将转换为导航层次结构中的现有视图控制器。 Callbacks are provided to both the source and destination view controller before the transition begins. 在转换开始之前,将向源视图控制器和目标视图控制器提供回调。 You can use these callbacks to pass data between the view controllers. 您可以使用这些回调在视图控制器之间传递数据。

Here's a example where the second (source) view controller presented a list of fonts, and the first (destination) view controller updates its table row to show the selected font: 下面是第二个(源)视图控制器显示字体列表的示例,第一个(目标)视图控制器更新其表行以显示所选字体:

- (IBAction)unwindFromFontPreference:(UIStoryboardSegue *)segue
{
    FontTableViewController *fontTableViewController = segue.sourceViewController;
    if (self.currentFontSelection != fontTableViewController.currentFontSelection)
    {
        self.currentFontSelection = fontTableViewController.currentFontSelection;
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:LALSettingsTableViewSectionFont]]
                              withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

FirstViewController.m FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()<UITableViewDelegate, UITableViewDataSource, ViewControllerDelegate>

@property (nonatomic, retain) NSMutableArray* data;
@property (nonatomic, retain) IBOutlet UITableView* tableView;
@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.data = [NSMutableArray array];
    [self.data addObject:@"country"];
    [self.data addObject:@"city"];
    // Do any additional setup after loading the view, typically from a nib.
}

- (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 self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    NSString* text = [self.data objectAtIndex:indexPath.row];

    cell.textLabel.text = text;
    return cell;
}

-(void) updateText:(NSString *)text
{
    [self.data replaceObjectAtIndex:1 withObject:text];
    [self.tableView reloadData];
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"secondView"])
    {
        UINavigationController* controller = [segue destinationViewController];
        NSArray *viewControllers = controller.viewControllers;
        SecondViewController* viewController = [viewControllers objectAtIndex:0];
        viewController.delegate = self;
    }
}

SecondViewController.h SecondViewController.h

#import <UIKit/UIKit.h>

@protocol ViewControllerDelegate <NSObject>

-(void) updateText:(NSString*)text;

@end
@interface SecondViewController : UIViewController

@property (nonatomic, assign) id<ViewControllerDelegate> delegate;
@end

SecondViewController.m SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@property(nonatomic ,retain) NSArray* detailFlights;
@end

@implementation SecondViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.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 [self.detailFlights count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.textLabel.text = [self.detailFlights objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{   NSString* text = [self.detailFlights objectAtIndex:indexPath.row];
    [self.delegate updateText:text];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

whole project is here 整个项目都在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 目标C-将JSON数据从Tableview传递到另一个View Controller - Objective C - Passing JSON data from Tableview to another View Controller 如何在目标C中将数据加载到自定义TableView单元内的选择器视图中 - how to load data to picker view inside custom tableview cell in ios, objective c 快速将TableView单元中的数据传递给视图控制器 - Swift Passing Data in a tableview cell to a view controller 如何将选定的collectionview单元格发送到另一个视图控制器(ios)? - How to send a selected collectionview cell to another view controller (ios)? 将数据从 tableview 单元格传递到 View Controller Swift 2.1 - Passing data from tableview cell to View Controller Swift 2.1 如何使用Objective C获取Tableview选定的单元格UILabels值? - How to get Tableview selected cell UILabels Value using Objective C? iOS-目标C-如何从登录视图控制器中删除Tableview菜单? - IOS - Objective C - How do I remove Tableview Menu from my Login View Controller? 如何在表格视图单元格上按以向视图控制器显示导航控制器中的文本 - How to press on a tableview cell to present a view controller with the text in navigation controller ios Swift:将数据从tableView传递到视图控制器 - ios Swift: Passing data from tableView to view controller 如何在tableview cell ios Objective c中实现页面控制 - How to implement page control in tableview cell ios objective c
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM