[英]Pass data back to Viewcontroller from tableview
I have ViewController and TableViewController. 我有ViewController和TableViewController。 In ViewController i have a 2 buttons and 2 textfields, when button1 is clicked it will navigate to UITableView with static data like (Apple, Samsung, Blackberry, Windows),and button2 is clicked it will navigate to UITableView static data (Doctor, Engineer,Businessman, Employee)when i select any of the row it should be displayed in the textfield1 of button1 clicked and textField2 of button2 clicked ViewController. 在ViewController中,我有2个按钮和2个文本字段,当单击button1时,它将导航到带有静态数据(如Apple,三星,Blackberry,Windows)的UITableView,并且单击button2时,它将导航到UITableView静态数据(医生,工程师,当我选择任何行时,应在button1的textfield1中单击并在button2的textField2中单击ViewController。 below is what i have tried as am learning, i don't know wheather it is correct or not. 以下是我一直在尝试的学习方法,我不知道它是否正确。
CustomerVC.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
- (IBAction)button1Click:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *txtField1;
- (IBAction)button2Click:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *txtField2;
CustomerTableVC.h
#import <UIKit/UIKit.h>
@interface DetailsViewController : UITableViewController
@end
CustomerTableVC.m
#import "DetailsViewController.h"
@interface DetailsViewController ()
{
NSArray *array1,*array2;
}
@end
@implementation FamilyDetailsViewController
- (void)viewDidLoad {
[super viewDidLoad];
array1=[[NSArray alloc]initWithObjects:@"Apple",@"Samsung",@"Blackberry",@"Windows", nil];
array2=[[NSArray alloc]initWithObjects:@"Doctor",@"Engineer",@"Businessman",@"Employee", nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array1 count];
return [array2 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[arrqy1 objectAtIndex:indexPath.row];
cell.textLabel.text=[arrqy2 objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.navigationController popViewControllerAnimated:YES];
}
You should use Unwind segue to pass data back from your tableview controller.. 您应该使用Unwind segue将数据从tableview控制器传回。
Steps to follow: 遵循的步骤:
Suppose A & B are two controllers and you first navigated from A to B with some data. 假设A和B是两个控制器,您首先使用一些数据从A导航到B。 And now you want to POP from B to A with some data. 现在,您想使用一些数据从B POP到A POP。
Unwind Segues is the best and recommended way to do this. 放松Segues是执行此操作的最佳建议。 Here are the steps. 步骤如下。
define following method 定义以下方法
@IBAction func unwindSegueFromBtoA(segue: UIStoryNoardSegue) { }
open storyboard 打开情节提要
Select B ViewController and click on ViewController outlet. 选择B ViewController,然后单击ViewController插座。 press control key and drag to 'Exit' outlet and leave mouse here. 按下控制键并拖动到“退出”出口,然后将鼠标留在此处。 In below image, selected icon is ViewController outlet and the last one with Exit sign is Exit Outlet. 在下图中,选中的图标是ViewController插座,最后一个带有Exit标志的图标是Exit Outlet。
You will see 'unwindSegueFromBtoA' method in a popup . 您将在弹出窗口中看到“ unwindSegueFromBtoA”方法。 Select this method . 选择此方法。
Now you will see a segue in your view controler hierarchy in left side. 现在,您将在左侧的视图控制器层次结构中看到一个序列。 You will see your created segue near StoryBoard Entry Piont in following Image. 下图将在StoryBoard Entry Piont附近看到您创建的序列。
Select this and set an identifier to it. 选择它并为其设置标识符。 (suggest to set the same name as method - unwindSegueFromBtoA) (建议设置与方法相同的名称-unwindSegueFromBtoA)
Open Bm . 打开Bm。 Now, wherever you want to pop to A. use 现在,无论您要弹出A的哪个位置,都可以使用
self.performSegueWithIdentifier("unwindSegueFromBtoA", sender: dataToSend)
Now when you will pop to 'A', 'unwindSegueFromBtoA' method will be called. 现在,当您弹出到“ A”时,将调用“ unwindSegueFromBtoA”方法。 In unwindSegueFromBtoA of 'A' you can access any object of 'B'. 在“ A”的unwindSegueFromBtoA中,您可以访问“ B”的任何对象。
That's it..! 而已..!
Try using delegate. 尝试使用委托。 1. Declare delegate on Tableview
controller's. 1.在Tableview
控制器上声明Tableview
。
In Custom Vc 在自订Vc中
FamilyViewController.delegate = self;
声明FamilyViewController.delegate = self;
self.view presentviewcontroller = familyviewController;
detailsselected()
) 定义委托函数(例如detailsselected()
) detailsSelected
dismissViewController
. 在detailsSelected
内部dismissViewController
。 In FailyDetailsViewController
: 在FailyDetailsViewController
:
didselectRowAtIndexPath :
call the " detailsSelected
" function and pass the selected values. 在didselectRowAtIndexPath :
内部didselectRowAtIndexPath :
调用“ detailsSelected
”函数并传递选定的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.