[英]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所述,必须实现委托方法将数据传输到第一视图,但该逻辑是选择objectAtIndex
的indexPath.row
在didSelect
: [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;
}
if you are using navigationController then 如果你正在使用navigationController
ViewController *previousViewController = (ViewController *) self.navigationController.viewControllers[self.navigationController.viewControllers.count-2];
previousViewController.selectedString = selectedString;
or 要么
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.