如何将选定的tableview单元格文本发送到iOS中的上一个视图控制器,目标C(向后传递数据)
我的应用程序中有两个UIView。 在第一个视图中,有一个带有两个单元格的TableVew(用于选择城市和国家)。当用户选择第一个单元格(选择城市)时,它将转到另一个包含城市列表的视图。然后,当用户选择一个城市(选择一个tableviecell文本)时,所选内容应显示在firtview的tableviewcell文本中。 这是我在secondview控制器(它是tableview控制器)中的代码如何将选定的tableview单元格文本发送到iOS中的上一个视图控制器,目标C(向后传递数据),ios,objective-c,uitableview,Ios,Objective C,Uitableview,我的应用程序中有两个UIView。 在第一个视图中,有一个带有两个单元格的TableVew(用于选择城市和国家)。当用户选择第一个单元格(选择城市)时,它将转到另一个包含城市列表的视图。然后,当用户选择一个城市(选择一个tableviecell文本)时,所选内容应显示在firtview的tableviewcell文本中。 这是我在secondview控制器(它是tableview控制器)中的代码 使用委托模式可以解决此类问题。在iOS编程中广泛使用的一种模式。看看您是否不知道它是如何工作的。正如
使用委托模式可以解决此类问题。在iOS编程中广泛使用的一种模式。看看您是否不知道它是如何工作的。正如deadbeef所说,您必须实现委托方法来将数据传输到第一个视图,但其逻辑是在
didSelect
中选择objectAtIndex
的indexath.row
:[detailFlights objectAtIndex:indexath.row]代码>在didDeselectRowAtIndexPath方法中,您可以使用以下方法获得该字符串
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *selectedString = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
}
现在保存以传递上一个视图控制器,您可以将其存储在UserDefaults或
在previousViewController中创建一个属性,并在返回之前将其设置为如下值
如果您使用的是navigationController,则
ViewController *previousViewController = (ViewController *) self.navigationController.viewControllers[self.navigationController.viewControllers.count-2];
previousViewController.selectedString = selectedString;
或
还可以使用块传递数据李>
作为其他人提到的委托方法的替代方法,可以使用将数据传递回上一个视图控制器
- (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];
}
“展开”序列为您提供了一种通过推送、模式、popover和其他类型的序列“展开”导航堆栈的方法。您可以使用展开分段“返回”导航层次结构中的一个或多个步骤。与普通segue不同,普通segue会创建其目标视图控制器的新实例并转换到该实例,而展开segue会转换到导航层次结构中的现有视图控制器。在转换开始之前,将向源视图控制器和目标视图控制器提供回调。可以使用这些回调在视图控制器之间传递数据
下面是一个示例,其中第二个(源)视图控制器显示字体列表,第一个(目标)视图控制器更新其表行以显示所选字体:
- (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
#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;
}
}
#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
整个项目是您应该实施
#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