简体   繁体   English

将数据从tableview传递回Viewcontroller

[英]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. 步骤如下。

  1. Open Am 营业时间
  2. define following method 定义以下方法

     @IBAction func unwindSegueFromBtoA(segue: UIStoryNoardSegue) { } 
  3. open storyboard 打开情节提要

  4. 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。

  5. You will see 'unwindSegueFromBtoA' method in a popup . 您将在弹出窗口中看到“ unwindSegueFromBtoA”方法。 Select this method . 选择此方法。

  6. 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附近看到您创建的序列。

您查看层次结构

  1. Select this and set an identifier to it. 选择它并为其设置标识符。 (suggest to set the same name as method - unwindSegueFromBtoA) (建议设置与方法相同的名称-unwindSegueFromBtoA)

  2. Open Bm . 打开Bm。 Now, wherever you want to pop to A. use 现在,无论您要弹出A的哪个位置,都可以使用

     self.performSegueWithIdentifier("unwindSegueFromBtoA", sender: dataToSend) 
  3. 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中

  1. Declare FamilyViewController.delegate = self; 声明FamilyViewController.delegate = self;
  2. self.view presentviewcontroller = familyviewController;
  3. Define the delegate function (eg. detailsselected() ) 定义委托函数(例如detailsselected()
  4. Inside the detailsSelected dismissViewController . detailsSelected内部dismissViewController

In FailyDetailsViewController : FailyDetailsViewController

  1. Inside the didselectRowAtIndexPath : call the " detailsSelected " function and pass the selected values. didselectRowAtIndexPath :内部didselectRowAtIndexPath :调用“ detailsSelected ”函数并传递选定的值。

暂无
暂无

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

相关问题 将数据从Tableview传递回ViewController文本字段Objective-C - Pass data back from tableview to viewcontroller textfield Objective - C TableView嵌套在CollectionViewCell中的VC,我如何将数据从TableView传递回ViewController - VC with TableView nested in CollectionViewCell, how can i pass data from TableView back to ViewController 将数据从ViewController传递回根ViewController? - Pass data from ViewController back to root ViewController? 如何将数据从视图控制器传递到另一个表视图? - How to pass data from a viewcontroller to another tableview? 将数组数据从ViewController传递到TableView - Pass arrays data from a ViewController to a TableView Swift-将数据从TableView传递到第三个ViewController - Swift - pass data from tableview to third viewcontroller iOS使用presentModalSegue将数据从viewController2传递回viewController 1 - iOS Pass data back from viewController2 to viewController 1 with presentModalSegue 我如何将数据从SlideTableTableViewController传递到ViewController中嵌入的TableView - How i pass Data from SlideOut TableViewController to TableView embedded in ViewController 将数据传回上一个视图控制器 - Pass data back to previous viewcontroller 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM